Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

Problem with pointers in a function

Ok (see the code below) I have written this little problem but I must be missing something. You should be able to see what I ma doing from the code. I am compiling on Linux with

gcc -Wall -ggdb -lm -O2 filename.c -o filename

I get two errors:

Partc.c: In function `main':
Partc.c:59: warning: passing arg 1 of `FindGradeAmmendCount' from incompatible pointer type
Partc.c:59: warning: passing arg 3 of `FindGradeAmmendCount' makes integer from pointer without a cast

The output from this function is messed up when called form main()

can someone show me what I need two change and tell me why - cheers!!

#include <stdio.h>

#define SUCCESS 0
#define ERROR 1

int GetNameAndMark (char p_surname[],
                char p_forename[],
                int *p_max_mark,
                int *p_mark)
{
        printf ("Please enter the students surname: ");
        scanf ("%s", p_surname);
        printf ("Please enter the students forename: ");
        scanf ("%s", p_forename);
        /* What the test is out of... */
        printf ("Enter the maximum mark of this test: ");
        scanf ("%d", &*p_max_mark);
        printf ("Now enter the students mark: ");
        scanf ("%d", &*p_mark);

        return SUCCESS;
}

int FindGradeAmmendCount (char p_pass[],
                        int p_max_mark,
                        int p_mark)
{
        if ((p_mark / p_max_mark) * 100 < p_max_mark / 50)
        {
                p_pass = "Failed";
        }
        else
        {
                p_pass = "Passed";
        }

        return SUCCESS;
}

int main (void)
{
char surname[20];
char forename[20];
char pass[6];
int max_mark;
int mark;

        if (GetNameAndMark (surname, forename, &max_mark, &mark))
        {
                printf ("Error calling function GetNameAndMark\n");
                return ERROR;
        }
        printf ("Student %s %s scored %d\n", forename, surname, mark);
       
        if (FindGradeAmmendCount (&max_mark, mark, &pass))
        {
                printf ("Error calling function FindGradeAmmendCount\n");
                return ERROR;
        }
        printf ("Student %s %s %s with %d out of %d\n", forename, surname, pass, mark, max_mark);

        return SUCCESS;

}
0
Jim2000
Asked:
Jim2000
  • 3
  • 2
  • 2
  • +3
1 Solution
 
jhanceCommented:
You've declared FindGradeAmmendCount as taking 3 arguments, a char * (a.k.a. char [], an int, and another int.

int FindGradeAmmendCount (char p_pas[], int p_max_mark, int p_mark)

So when you call it here:

if (FindGradeAmmendCount (&max_mark, mark, &pass))

what are the TYPES of the arguments you are passing to it?

The compiler says that what you've said the function takes is DIFFERENT from what you are passing to it and there is no possible automatic conversion that it can do that makes sense.  Hint, the compiler is even telling you that the arguments in question are 1 and 3.


0
 
Jim2000Author Commented:
Eh!? Not quite with you here. I know what my types are (a char array (string) and two ints).

The if statement is just used to return a status nothing is passed to the function. Obviously all these errors are at compile time. Can you give me a little more of a hint I don't think I quite get what your getting at!
0
 
jkrCommented:
jhance is almost right...

You've simply mixed up the order of arguments (and made a little 'indirectional' mistake):

        if (FindGradeAmmendCount (&max_mark, mark, &pass))

is a call to a function that is declared as  'int* , int , char**'

where 'char*, int , int' is required.

So, using

        if (FindGradeAmmendCount (pass, max_mark, mark))

should remedy the error messages ...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
cookreCommented:
jhance is just trying to help you find the problem on your own - the only way to learn anything - so I'm not going to poach on his perfectly reasonable response.

What he really was asking was: In the call to FindGradeAmmendCount, what are the types of the values actually being passed - not the types of the variables, but the actual argument, i.e., after the expressions in the call have been evaluated?
0
 
rbrCommented:
use  
 if (FindGradeAmmendCount (pass,max_mark, mark))
                                     
Another mistake
int FindGradeAmmendCount (char p_pass[],
int p_max_mark,int p_mark)
                                 
{
if ((p_mark / p_max_mark) * 100 < p_max_mark / 50)
                                          {
                                                  p_pass = "Failed";
                                          }
                                          else
                                          {
                                                  p_pass = "Passed";
                                          }

                                          return SUCCESS;
               
                  }
will not work
use
strcpy (p_pass,"Failed");
and
strcpy (p_pass,"Passed");
0
 
jhanceCommented:
rbr,

I appreciate your stomping in on my answer.  Thanks...  It should also be clear to you that this is a homework problem of some sort.  While it is against EE policy for experts to do homework for those asking the questions, it _IS_ OK to ask for clarification on a problem.

I was trying to get Jim2000 to think about this a bit. You not only are stealing this question but are doing this guy's homework.

You, as a long time expert here, ought to know better!
0
 
Jim2000Author Commented:
I'm gonna reject this answer / many responses because I believe what jhance is syaing to rbr is right. Even though this is a *snippet* of my homework I can't get the bugger to work I got it to compile ok with ho errors but my pointers / mem access are still messed up. I edited my code last night to get it to compile but when I use the strcpy function (yes I did include strings.h) the printf statement sure it printed out the "passed" or "failed" string but the numbers where totally screwed up. Below is the code I came up with that compiles but still doesn't work (aahh)!!

snip...

int FindGradeAmmendCount (char p_pass[],
                        int p_max_mark,
                        int p_mark)
{
char *pass_switch = "Passed";

        if ((p_mark / p_max_mark) * 100 < p_max_mark / 50)
        {
                pass_switch = "Failed";
                p_pass = pass_switch;
        }
        else
        {
                p_pass = pass_switch;
        }

        return SUCCESS;

snip...

/* main function now */

char [7];

if (FindGradeAmmendCount (pass, max_mark, mark))
        {
                printf ("Error calling function FindGradeAmmendCount\n");
                return ERROR;
        }
        printf ("Student %s %s %s with %d out of %d\n", forename, surname, pass, mark, max_mark);

        return SUCCESS;

input = Bloggs, Joe, 30, 15
output = Student Joe Bloggs *èä@JJoe with 15 out of 30

OR

input = Bloggs, Joe, 30, 15
using strcpy returns output =
Student Joe Bloggs *èPassed with 100 out of 24398709732

What's going on!!????
0
 
MDarlingCommented:
pass_switch is local - will it still be ok after you have exited the function?

and if not - what is pointing at it?

regards,
Mike.
0
 
rbrCommented:
use at least

char pass[7];

 
strcpy (p_pass,"Failed");
and
strcpy (p_pass,"Passed");

and
scanf ("%d", p_max_mark);

scanf ("%d", p_mark);
0
 
Jim2000Author Commented:
Well I might as well stop this posting I figured the strcpy problem about 10 mins after I posted this but now something else is bothering me. Cheers to everyone You should all get these points - what about EE developers thinking about adding an option to choose other answers/comments to award points to after rejection of another!

Cheers

Jim

ps. I fixed it with this...

int FindGradeAmmendCount (char *p_pass,
                  int *p_pass_count,
                  int p_max_mark,
                        int p_mark)
{
      if ((p_mark / p_max_mark) * 100 >= p_max_mark / 2)
      {
            strcpy (p_pass, "Passed");
            *p_pass_count = *p_pass_count + 1;
      }
      else
      {
            strcpy (p_pass, "Failed");
      }


snip...

if (FindGradeAmmendCount (pass, &pass_count, max_mark, mark))
            {
                  printf ("Error calling function FindGradeAmmendCount\n");
                  return ERROR;
            }
            printf ("Student %s %s %s with %d out of %d\n", forename, surname, pass, mark, max_mark);


-- but it still says failed even though they should have passed!! Oh well!
0
 
MDarlingCommented:
if((p_mark/p_max_mark)*100 >= p_max_mark/2)
{
....
}

are you not comparing apples with oranges?

the left hand side of >= is a percentage
is the right?

regards,
Mike.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now