Problem with "pointer from integer without a cast"

This is a discussion on Problem with "pointer from integer without a cast" within the C Programming forums, part of the General Programming Boards category; Hi, I have yet another problem after spending a full day today working on my program. What I am trying ...

  1. #1
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56

    Problem with "pointer from integer without a cast"

    Hi, I have yet another problem after spending a full day today working on my program. What I am trying to do is to read a textfile, and put the values within the textfile into proper categories within the struct. So in this case I have built a struct containing the members, which are categories that I want the values to be in.
    Here is my struct:
    Code:
    struct database_input {
      int categoryA;
      int categoryB;
      double categoryC;
      int categoryD;
    
    };

    where the format of one line of the data
    (the textfile consists of alot of lines of the same format):
    1.3.1.2 = 3.97438e-05 121 1
    in which I want the first number (1.3.1.2) to go into the first category and the second number (3.97438e-05) to go in the 2nd category and so on.
    What I did to get 1.3.1.2 to be and int is to establish a loop to skip the periods and only read 1312 as a string, then typecast it back to int. For the second number, since everything is in e-05, I only read the number up to 3.97438, and then I typecast to double and divide by 10000.

    Here is how I call the function in main:
    Code:
    int main()
    {
    struct database_input data[99999];
    initializing_structs(data); //for setting all values to 0
    read_input( inputname , data);
    ...
    the function code is:

    Code:
    void read_input( FILE *ifp, struct database_input *data)
    {
    int i=0;
    char number = NULL;
    char *categoryA_number = NULL;
    
    char  *categoryC_f = NULL;
    double categoryC_number;
    
    char *categoryB_number;
    
    while( i<99999 && feof(ifp) != 1)
    	{
    	while( number != ' ')
    	{
    		fscanf( ifp, number);
    		if (number != '.')
    		strcat(categoryA_number, number);
    	}
    
    	data[i].categoryA = (int)*categoryA_number;
    	ifp = ifp+2;
    	number = NULL;
    
    	while( number != 'e')
    	{
    		fscanf( ifp,number);
    		strcat(categoryC_f, number);
    	}
    	
    	categoryC_number = (double)*categoryC_f;
    	data[i].categoryC = categoryC_number/10000;
    	ifp = ifp+5;
    	number = NULL;
    	
    	while( number != ' ')
    	{
    		fscanf( ifp, number);
    		if (number != '.')
    		strcat(categoryB_number, number);
    	}
    
    	
    	data[i].categoryB = (int)*categoryB_number;
    	ifp = ifp+1;
    	number = NULL;
    	fscanf( ifp, number);
    	data[i].categoryD = (int)number;
    	number = NULL;
    	
    	while( number != '\n')
    	{
    		fscanf( ifp, number);
    		ifp++;
    	}
    	
    	i++;
    	}
    }
    I know this is a long post, but please keep on reading...

    The program when compiled, had many warnings, like:
    projectmain.c:91: warning: conflicting types for ‘initializing_structs’
    projectmain.c:54: warning: previous implicit declaration of ‘initializing_structs’ was here
    projectmain.c:107: warning: conflicting types for ‘read_input’
    projectmain.c:55: warning: previous implicit declaration of ‘read_input’ was here
    projectmain.c: In function ‘read_input’:
    projectmain.c:109: warning: initialization makes integer from pointer without a cast
    projectmain.c:121: warning: passing argument 2 of ‘fscanf’ makes pointer from integer without a cast
    projectmain.c:123: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast
    projectmain.c:128: warning: assignment makes integer from pointer without a cast
    projectmain.c:132: warning: passing argument 2 of ‘fscanf’ makes pointer from integer without a cast
    projectmain.c:133: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast
    projectmain.c:139: warning: assignment makes integer from pointer without a cast
    projectmain.c:143: warning: passing argument 2 of ‘fscanf’ makes pointer from integer without a cast
    projectmain.c:145: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast
    projectmain.c:152: warning: assignment makes integer from pointer without a cast
    When I run the program, it just went into segmentation error. I already tried to simplify the function as much as possible, but it still ends up with a long code. Can somebody please help me to find what was wrong with this code, and if possible, ways to fix it? Since the code that I have wrote is a bit confusing, don't hold back in asking me questions about how the logic of the code works.

    as for not feof(), I am having difficulties in inplementing the fix in the faq into my code since I am not reading the whole line, I am reading from character to character.

    Many Thanks.
    Last edited by firyace; 05-11-2007 at 08:42 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Sounds like an issue with default promotions. Do you prototype the function before you call it? The line numbers are meaningless without full context, BTW.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    The first 3 almost certainly relate to not having a prototype for the function before trying to call it.

    All the rest relate to your completely broken fscanf calls.

    > struct database_input data[99999];
    You might want to reconsider putting such a large array on the stack.

    > while( i<99999 && feof(ifp) != 1)
    1. Don't use feof() in a control loop - see the FAQ
    2. Who said it returned specifically 1 or not 1. The results are zero and non-zero.
    3. The 99999 should have been passed as another parameter to the function, or at the very least made a constant for the whole file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    Hi, thankyou for the reply.
    Yeah, I forgot the prototype because I have put them in a separate header file and forgot to include the header file itself. Well now I have put the prototypes back to the main file (which I will erase once I finish everything else and include my header).
    Now the error is like this:
    projectmain.c:10: warning: "struct database_input" declared inside parameter list
    projectmain.c:10: warning: its scope is only this definition or declaration, which is probably not what you want
    projectmain.c:11: warning: "struct database_input" declared inside parameter list
    projectmain.c:14: warning: "struct database_input" declared inside parameter list
    projectmain.c:15: warning: "struct database_input" declared inside parameter list
    projectmain.c:16: warning: "struct database_input" declared inside parameter list
    projectmain.c: In function `main':
    projectmain.c:62: warning: passing arg 1 of `read_input' from incompatible pointer type
    projectmain.c:65: warning: unused variable `vertex'
    projectmain.c: In function `read_input':
    projectmain.c:116: warning: initialization makes integer from pointer without a cast
    projectmain.c:128: warning: passing arg 2 of `fscanf' makes pointer from integer without a cast
    projectmain.c:130: warning: passing arg 2 of `strcat' makes pointer from integer without a cast
    projectmain.c:135: warning: assignment makes integer from pointer without a cast
    projectmain.c:139: warning: passing arg 2 of `fscanf' makes pointer from integer without a cast
    projectmain.c:140: warning: passing arg 2 of `strcat' makes pointer from integer without a cast
    projectmain.c:146: warning: assignment makes integer from pointer without a cast
    projectmain.c:150: warning: passing arg 2 of `fscanf' makes pointer from integer without a cast
    projectmain.c:152: warning: passing arg 2 of `strcat' makes pointer from integer without a cast
    projectmain.c:158: warning: assignment makes integer from pointer without a cast
    projectmain.c:159: warning: passing arg 2 of `fscanf' makes pointer from integer without a cast
    projectmain.c:161: warning: assignment makes integer from pointer without a cast
    I have no idea why, but new errors came about. Also how can I fix my broken fscanf and strcat? I have change stuff such as making it from adding a *before the arguments but nothing worked. Also, with the warning
    assignment makes integer from pointer without a cast
    ,
    one of the lines are like this:
    Code:
    number = NULL;
    if I can't set a character to NULL, what values should I set to so that it won't come into conflict with the code?

    I have to set the array size to 99999 because I am dealing with a very large amount of data, so I have to set the array to the max number, from which I have tested to be 99999.

    Many Thanks.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You're doing something like this:

    Code:
    int x;
    ....
    scanf("%d",x); /* WRONG */
    You should be doing:

    Code:
    int x;
    ....
    scanf("%d",&x); /* Right */

  6. #6
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    thanks for your fast reply and sharp eyes, I can't believe I skipped that while reading it 3 times. I thought that I was dealing with a char pointer instead. One of my really silly mistakes when looking at variables with the same type. An because of this, I fixed the number = NULL part. Man, I hate my noobishness for first made it a char type and treated as a char pointer for the rest of the program (not only in this function).
    Last edited by firyace; 05-11-2007 at 08:59 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > projectmain.c:10: warning: "struct database_input" declared inside parameter list
    Move the "struct database_input..." declaration above where you first attempt to use it


    Code:
    struct foo {
      int member;
    };
    void func ( struct foo *bar );
    Not
    Code:
    void func ( struct foo *bar );  // this will draw that first warning
    struct foo {
      int member;
    };
    To be honest, you need to read your C book some more.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  3. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 03:28 PM
  4. "assignment makes integer from pointer without a cast"
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-04-2002, 03:26 AM
  5. problem with random integer
    By techno logic in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2002, 02:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21