Link to home
Start Free TrialLog in
Avatar of Jim2000
Jim2000

asked on

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;

}
Avatar of jhance
jhance

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.


Avatar of Jim2000

ASKER

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!
Avatar of jkr
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 ...
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?
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");
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!
Avatar of Jim2000

ASKER

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!!????
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.
ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim2000

ASKER

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!
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.