[Warning]assignment makes integer from pointer without a cast

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shane Fitz
    New Member
    • Nov 2010
    • 4

    [Warning]assignment makes integer from pointer without a cast

    The title is the warning I get when I try to run the following function

    Code:
    char * setup_mutations()
    {
    	char *result = malloc(sizeof(char));
    	int i;
    	char c, current, string[MAX_STRING];
    	FILE * fp;
    	
    	if((fp = fopen("mutations.txt","r"))==NULL){
    		printf("Mutations file not available\n");
    		exit(2);
    	}
    	
    	for(i=0; i<256; i++){
    		//result[i] = malloc(sizeof(char));
    		result[i] = '\0';
    	}
    	
    	while ( (c = fgetc(fp) ) != EOF ){
    		current = c;
    		
    		for(c=fgetc(fp); c!=' '; c=fgetc(fp)){
    			
    		}
    		
    		i=0;
    		
    		for(c=c; c!='\n'; c=fgetc(fp)){
    			string[i] = c;
    			i++;
    		}
    		i = current;
    		result[i] = string;
    	}
    	
    	if((fclose(fp))==-1)printf("Mutations file didn't close properly\n");
    	return result;
    };
    It comes from the line 32. Any help that helps me fix this problem and understand why it has arisen is appreciated.

    Thanks
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    1. Line 3 establishes result as a pointer to a single char; however line 15 seems to think that result points to 256 chars. It doesn't.
    2. Lines 21 and 27. Consider what happens if fgetc returns EOF.
    3. Line 21. It is customary for no-body for loop to have a single bare semicolon as the body.
    4. Line 28. How do you know you won't write past the end of string?
    5. Line 32. result is a pointer to a single char; thus result[i] is a char. This line tries to store a pointer-to-char there. That's why you get that warning.

    Comment

    Working...