Thread: Pointer without a cast error

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    19

    Pointer without a cast error

    I've been playing around with this sorting program and got an error message while compiling it:

    "[Warning] passing arg 2 makes integer from pointer without a cast"

    The program in question follows:

    Code:
    /* upBal.c - update balances */
    
       #include <stdio.h>
       #include <stdlib.h>
       #define MAX_CUSTOMERS 100
       struct custRec
       {
       	int ID;
       	float Bal;
       };
      /* prototypes of functions */
      int readInput(struct custRec *, char *);
      void sortData(struct custRec *, int);
      void printRecords(struct custRec*, int);
      void upDateBalances(struct custRec*, int);
      int main()
      {
      	int noOfRecords;
      	struct custRec custs[MAX_CUSTOMERS];
      	struct custRec upBal[MAX_CUSTOMERS];
      	/* read the input */
      	noOfRecords = readInput(custs, "cust.dat");
      	noOfRecords = readInput(upBal,"update.dat");
      	/* sort the both arrays */
      	sortData(custs,noOfRecords);
      	sortData(upBal,noOfRecords);
      	/* update the balances */
     	upDateBalances(custs,upBal);
      	/* print the updated values */
      	printf("Ater updating\n");
      	printRecords(custs,noOfRecords);
      	return 0;
      }
      
      /* read the input file and put data into the
      	array that records points at  */
      int readInput(struct custRec *records, char *fileName)
      {
      	FILE *input = fopen(fileName,"r");
      	int noOfRecords = 0;
      	char tempLane[240];
      	int tempCustId;
      	float tempCustBal;
      	if ( input == 0)	/* can't open filename */
      	{
      		fprintf(stderr,"Can not open %s\n", fileName);
      		fprintf(stderr,"please check\n\n");
      		exit(-1);
      	}
      	while (fgets(tempLane,240,input))
      	{
      		/* if the first character is a "#" ignore */
      		if (tempLane[0] == '#')
      		{
      			continue;
      		}
      		/* make sure that the line has 2 numbers */
      		if (sscanf(tempLane,"%d, %f",
      				&tempCustId, &tempCustBal) == 2)
      		{
      			records[noOfRecords].ID = tempCustId;
      			records[noOfRecords].Bal = tempCustBal;
      			noOfRecords++;
      		}
      	}
      	fclose(input);
      	return noOfRecords;
      }
    
    /* sort the records */
      void sortData(struct custRec *custs, int noOfRecs)
      {
      	int inner, outer, didSwap;
      	struct custRec temp;
      	/* sort the array */
      	for (outer = 1; outer < noOfRecs; outer++)
      	{
      		didSwap = 0;
      		for (inner = noOfRecs-1; inner >= outer ; inner--)
      		{
      			if (custs[inner-1].ID > custs[inner].ID )
      			{
      				temp = custs[inner-1];
      				custs[inner-1] = custs[inner];
      				custs[inner] = temp;
      				didSwap = 1;
      			}
      		} /* inner loop */
      		if (didSwap == 0)
      		{
      			break;
      		}
      	} /* outer loop */
      	return ;
      }
      void printRecords(struct custRec* custs, int noOfRecs)
      {
      	int i;
      	/* print values */
      	printf("Cust ID    Balance\n");
      	for (i = 0; i < noOfRecs ; i++)
      	{
     		printf("%5d %12.2f\n",
     			custs[i].ID, custs[i].Bal);
     	}
     	return;
     }
    
     void upDateBalances(struct custRec* custs, struct custRec* newBalances, int noOfRecords)
     {
     	int i;
     	for (i = 0 ; i < noOfRecords; i++)
    	{
     		custs[i].Bal += newBalances[i].Bal;
     	}
     	return;
     }
    I believe that the problem lies in the 'upDateBalances' function because the program will compile when I comment it out as well as its mention within the main program.

    The compiling error highlights the piece in the main program

    Code:
    upDateBalances(custs,upBal);
    I'm sure that it's something trivial but yet I cannot seem to see what's wrong. If anyone has any ideas, I'd much appreciate any input. Thanks.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well that function takes three arguments and you are passing two.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by claudiu View Post
    Well that function takes three arguments and you are passing two.
    also the function prototype is different from function definition...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, vart is correct. I hadn't even looked at that.

    @OP you need to declare, define and call the function in a "unified" fashion.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    19
    Quote Originally Posted by claudiu View Post
    Well that function takes three arguments and you are passing two.
    Ah, so the function call would NOT read

    Code:
    upDateBalances(custs,upBal);
    but instead

    Code:
    upDateBalances(custs,newBalances, noOfRecords);
    which would be three arguments rather than two.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    19
    Quote Originally Posted by claudiu View Post
    Yes, vart is correct. I hadn't even looked at that.

    @OP you need to declare, define and call the function in a "unified" fashion.
    Thanks claudiu. What do you mean by 'unified' fashion?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mhunt007
    What do you mean by 'unified' fashion?
    I think that claudiu just means that the declaration of a function and its definition must be compatible. You cannot declare the function as having exactly two parameters, then define it as having exactly three parameters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by laserlight View Post
    I think that claudiu just means that the declaration of a function and its definition must be compatible. You cannot declare the function as having exactly two parameters, then define it as having exactly three parameters.
    Yes, thanks for clearing that out laserlight.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    19
    Quote Originally Posted by laserlight View Post
    I think that claudiu just means that the declaration of a function and its definition must be compatible. You cannot declare the function as having exactly two parameters, then define it as having exactly three parameters.
    So I am defining the function to have three parameters

    Code:
    void upDateBalances(struct custRec* custs, struct custRec* newBalances, int noOfRecords)
    but declaring the function to have only two

    Code:
    upDateBalances(custs,upBal);
    Is that what you're referring to? I tried to remove the 'noOfRecords' reference from the defined function but that did not work for me.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mhunt007
    Is that what you're referring to?
    Not quite. This is a forward declaration of upDateBalances:
    Code:
    void upDateBalances(struct custRec*, int);
    This is a call of upDateBalances:
    Code:
    upDateBalances(custs,upBal);
    This is the definition of upDateBalances:
    Code:
    void upDateBalances(struct custRec* custs, struct custRec* newBalances, int noOfRecords)
    {
        int i;
        for (i = 0 ; i < noOfRecords; i++)
        {
            custs[i].Bal += newBalances[i].Bal;
        }
        return;
    }
    Quote Originally Posted by mhunt007
    I tried to remove the 'noOfRecords' reference from the defined function but that did not work for me.
    Well, you cannot just remove the noOfRecords parameter because you are actually using it. Rather, it looks like you should change the declaration and function call to match the definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    19
    Quote Originally Posted by laserlight View Post
    Not quite. This is a forward declaration of upDateBalances:
    Code:
    void upDateBalances(struct custRec*, int);
    This is a call of upDateBalances:
    Code:
    upDateBalances(custs,upBal);
    This is the definition of upDateBalances:
    Code:
    void upDateBalances(struct custRec* custs, struct custRec* newBalances, int noOfRecords)
    {
        int i;
        for (i = 0 ; i < noOfRecords; i++)
        {
            custs[i].Bal += newBalances[i].Bal;
        }
        return;
    }

    Well, you cannot just remove the noOfRecords parameter because you are actually using it. Rather, it looks like you should change the declaration and function call to match the definition.
    I tried to match the forward declaration

    Code:
    void upDateBalances(struct custRec *,struct custRec *, int);
    with the call

    Code:
    upDateBalances(custs,upBal,noOfRecords);
    to match the definition

    Code:
    void upDateBalances(struct custRec* custs, struct custRec* newBalances, int noOfRecords)
    So now the program compiles successfully but does not work if that makes any sense.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    19
    There is no console which usually pops up which tells you what the program is doing and also says, "Press and key to exit..."
    Also, the output.dat file is empty.
    Last edited by mhunt007; 04-08-2010 at 03:11 PM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Run your program from a command prompt.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    19
    Okay. The program runs and the command window just states

    After sorting
    Cust ID Bal

    Press any key to exit...

    I didn't see anything in the output.dat file. I removed the comma between the '%d, %f' in the sscanf which enabled me to read the screen output on another program but it doesn't seem to be working for this one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM