Solved

Problem with pointers in a function

Posted on 2000-02-15
11
181 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
ID: 2525053
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
ID: 2525255
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
ID: 2525373
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
ID: 2525400
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
ID: 2526050
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
Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

 
LVL 32

Expert Comment

by:jhance
ID: 2526478
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
ID: 2526944
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
ID: 2527054
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
ID: 2527069
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
ID: 2528441
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
ID: 2530394
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

910 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

22 Experts available now in Live!

Get 1:1 Help Now