Solved

Problem with pointers in a function

Posted on 2000-02-15
11
180 Views
Last Modified: 2010-04-15
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
Comment
Question by:Jim2000
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
 

Author Comment

by:Jim2000
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
 
LVL 10

Expert Comment

by:rbr
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
 

Author Comment

by:Jim2000
Comment Utility
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
 
LVL 3

Expert Comment

by:MDarling
Comment Utility
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
 
LVL 10

Accepted Solution

by:
rbr earned 50 total points
Comment Utility
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
 

Author Comment

by:Jim2000
Comment Utility
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
 
LVL 3

Expert Comment

by:MDarling
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now