error: dereferencing pointer to incomplete type

This is a discussion on error: dereferencing pointer to incomplete type within the C Programming forums, part of the General Programming Boards category; Hi, I get the error above and I'm not sure why. I have a file img.h (which I didn't write ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    13

    error: dereferencing pointer to incomplete type

    Hi,

    I get the error above and I'm not sure why.
    I have a file img.h (which I didn't write and have to use), in which I have
    Code:
    typedef struct{
                 int XSize, YSize;
                 int ZSize;
                 unsigned char *Data;
                 }  imginfo;
    Then in img.c I have a function called LoadImage.
    My problem is that when I call it from another file, the function works fine (it prints out info about an image), but I don't know how to access the members.

    I'm doing this:
    Code:
    struct imginfo *infoCoul;
    int diggity;
    
    diggity = LoadImage(refCam, infoCoul);
    printf("X: %d\n", infoCoul->XSize);
    The prototype for LoadImage is:
    int LoadImage(char *Name, imginfo *I);
    It's not a missing .h file inclusion since my LoadImage function works.
    Also, if I wanted to acess *Data would I need to declare something like an "unsiged char **dataAccess"?

    Thanks
    Last edited by surlyTomato; 08-21-2009 at 05:06 AM. Reason: added explanation

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    What you are doing looks correct to me, except that struct imginfo should be imginfo.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by surlyTomato View Post
    Hi,

    I get the error above and I'm not sure why.
    I have a file img.h (which I didn't write and have to use), in which I have
    Code:
    typedef struct{
                 int XSize, YSize;
                 int ZSize;
                 unsigned char *Data;
                 }  imginfo;
    Then in img.c I have a function called LoadImage.
    My problem is that when I call it from another file, the function works fine (it prints out info about an image), but I don't know how to access the members.

    I'm doing this:
    Code:
    struct imginfo *infoCoul;
    int diggity;
    
    diggity = LoadImage(refCam, infoCoul);
    printf("X: %d\n", infoCoul->XSize);
    The prototype for LoadImage is:
    int LoadImage(char *Name, imginfo *I);
    It's not a missing .h file inclusion since my LoadImage function works.
    Also, if I wanted to acess *Data would I need to declare something like an "unsiged char **dataAccess"?

    Thanks
    I hope that before using the

    imginfo *infoCoul;

    you have assigned the address to the structure which you want to access.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    13
    Now it says
    error: ‘imginfo’ undeclared (first use in this function)

    imginfo is declared in img.h and I have a #include img.h in the file with the main.
    LoadImage (included in img.c), which is printing out the loaded image parameters, was working before I removed the "struct", like laserlight pointed out, and then it didn't anymore.

    In the file which I run to link the stuff (compilegcc) I have
    Code:
    gcc -o helperFunctions.o -c helperFunctions.c 
    gcc -o fonctionsCommandes.o -c fonctionsCommandes.c
    gcc -o img.o -c img.c 
    gcc -o matrixmath.o -c matrixmath.c
    gcc -Wall -o $1 $a helperFunctions.o fonctionsCommandes.o img.o matrixmath.o -lm
    and I use it by running
    ./compilegcc myFile
    where myFile is the one with the main. I included all the ".h"s in myFile.

    If I put a
    #include img.h
    in the specific file I need it in, it works, but I don't see why I should do it, since it's included in my main.

    If I comment out the above #include and re-add the incorrect "struct", it prints out:
    Code:
    Image x and y size in pixels: 100 100
    Image zsize in channels: 3
    Image pixel min and max: 0 255
    so I don't understand how it knows where to find LoadImage and not know where to find imginfo.

    Basically, I don't know if really need to include the "img.h" in each file I need it in, or if it's something else.

    Thanks.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by surlyTomato View Post
    Now it says
    error: ‘imginfo’ undeclared (first use in this function)

    imginfo is declared in img.h and I have a #include img.h in the file with the main.
    LoadImage (included in img.c), which is printing out the loaded image parameters, was working before I removed the "struct", like laserlight pointed out, and then it didn't anymore.

    In the file which I run to link the stuff (compilegcc) I have
    Code:
    gcc -o helperFunctions.o -c helperFunctions.c 
    gcc -o fonctionsCommandes.o -c fonctionsCommandes.c
    gcc -o img.o -c img.c 
    gcc -o matrixmath.o -c matrixmath.c
    gcc -Wall -o $1 $a helperFunctions.o fonctionsCommandes.o img.o matrixmath.o -lm
    and I use it by running
    ./compilegcc myFile
    where myFile is the one with the main. I included all the ".h"s in myFile.

    If I put a
    #include img.h
    in the specific file I need it in, it works, but I don't see why I should do it, since it's included in my main.

    If I comment out the above #include and re-add the incorrect "struct", it prints out:
    Code:
    Image x and y size in pixels: 100 100
    Image zsize in channels: 3
    Image pixel min and max: 0 255
    so I don't understand how it knows where to find LoadImage and not know where to find imginfo.

    Basically, I don't know if really need to include the "img.h" in each file I need it in, or if it's something else.

    Thanks.
    The reason it says imginfo undeclared is because its not finding the declaration for it and still you are trying to access it.

    Also the .h file should always be included where it is required (in the appropriate .c file) and not necessarily in the main file.

    Also post the .c file that is giving you the error.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    13
    The img.h file is also included in img.c
    The file that calls LoadImage is like this now
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "fonctionsCommandes.h"
    #include "img.h"
    
    
    void couleurd(char* methode, char* refCam, int x, int y, int z_d){
    	
    	imginfo *infoCoul;
    	//unsigned char *Data;
    	int diggity;
    	//int *Xsz;
    	
    	if(strcmp(methode,"plusproche")==0){
    		printf("refcam: %s, x: %d y: %d z:%d \n\n",refCam,x,y,z_d);
    		diggity = LoadImage(refCam, infoCoul);
    		//Data = infoCouleurd->Data;
    		//Xsz = infoCouleurd->XSize;
    		printf(">X: %d\n", infoCoul->XSize);
    
    	}
    	return;
    }
    }
    And a couple more prototypes below. Its name is fonctionCommandes.c and the couleurd() function is called from helperFunctions.c

    The output now is:
    Code:
    Image x and y size in pixels: 100 100
    Image zsize in channels: 3
    Image pixel min and max: 0 255
    >X: 100

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    13
    And, as an aside, if I do this
    Code:
           unsigned char *DataC;
    	int diggity;
    	//int *Xsz;
    	
    	if(strcmp(methode,"plusproche")==0){
    		printf("refcam: %s, x: %d y: %d z:%d \n\n",refCam,x,y,z_d);
    		diggity = LoadImage(refCam, infoCoul);
    		DataC = infoCoul->Data;
    		//Xsz = infoCouleurd->XSize;
    		printf(">X: %d\n", infoCoul->XSize);
    		printf(">Data_0: %s\n", DataC[0]);
    	}
    I get a seg fault. Isn't Data a string in the struct?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by surlyTomato View Post
    I get a seg fault. Isn't Data a string in the struct?
    No. It is something that could, one day, point to a string, if you happened to have one hanging around somewhere. But (the way you have things set up) you do not get a string free with every struct; they need to come from elsewhere.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    13
    If I comment the blah line out, it's fine. -- [edit: this part is fixed]

    Also, I'm sorry, but I don't see what you mean by "you do not get a string free with every struct", since Data is filled by LoadImage().
    Code:
    int LoadImage(char *Name,imginfo *I)
    {
    FILE *img;
    unsigned char *Data;
    /* Data : [XSize*YSize*ZSize)], Acces=[(Y*XSize+X)(*Zsz)+(z)] */
    int P,x,y,z,v;
    int XSize,YSize,ZSize,Ascii;
    char TBuf[100];
    
    	/* default values */
    	I->XSize=I->YSize=I->ZSize=0;
    	I->Data=NULL;
    
            img=fopen(Name,"r");
            if( img==NULL ) {
                    fprintf(stderr,"LoadImage: Unable to open '%s'\n",Name);return(-1);
            }
    
    	/** Read header **/
    	if( fgets(TBuf,100,img)!=TBuf ) {
                    fprintf(stderr,"LoadImage: Unable to read header of '%s'\n",Name);
    		return(-1);
    	}
    	if( strncmp(TBuf,"P5",2)==0 ) { Ascii=0;ZSize=1; }	/* pgm raw */
    	else if( strncmp(TBuf,"P6",2)==0 ) { Ascii=0;ZSize=3; }	/* ppm raw */
    	else if( strncmp(TBuf,"P2",2)==0 ) { Ascii=1;ZSize=1; }	/* pgm ascii */
    	else if( strncmp(TBuf,"P3",2)==0 ) { Ascii=1;ZSize=3; }	/* ppm ascii */
    	else {
    		fprintf(stderr,"LoadImage: Unsupported PNM format for '%s'\n",Name);
    		return(-1);
    	}
    	/* Skip comments */
    	do {
    		if( fgets(TBuf,100,img)!=TBuf ) return(-1);
    	} while( TBuf[0]=='#' );
    	/* xres/yres */
    	sscanf(TBuf,"%d %d",&XSize,&YSize);
    	/* Number of gray , usually 255, Just skip it */
    	if( fgets(TBuf,100,img)!=TBuf ) return(-1);
    
            /* print a little info about the image */
    #ifdef VERBOSE
            printf("Image x and y size in pixels: %d %d\n"
                    ,XSize,YSize);
            printf("Image zsize in channels: %d\n",ZSize);
            printf("Image pixel min and max: %d %d\n",0,255);
    #endif
    
            /* Allocate data space , as char data... for now */
            Data=(unsigned char *)malloc(XSize*YSize*ZSize);
            if( Data==NULL ) { fprintf(stderr,"LoadImage: Out of mem\n");return(-1); }
    
    	if( Ascii==0 ) {
    		for(y=0;y<YSize;y++) {
    #ifdef INVERSE_Y
    			P=(YSize-1-y)*XSize*ZSize;
    #else
    			P=y*XSize*ZSize;
    #endif
    			fread(Data+P,1,XSize*ZSize,img);
    		}
    	}else{
    		for(y=0;y<YSize;y++)
    		for(x=0;x<XSize;x++)
    		for(z=0;z<ZSize;z++) {
    #ifdef INVERSE_Y
    			P=((YSize-1-y)*XSize+x)*ZSize+z;
    #else
    			P=(y*XSize+x)*ZSize+z;
    #endif
    			fscanf(img," %d",&v);
    			Data[P]=v;
    		}
    	}
    
            I->Data=Data;
            I->XSize=XSize;
            I->YSize=YSize;
            I->ZSize=ZSize;
    
            fclose(img);
            return(0);
    }
    Doesn't that mean that Data points to the pixel values in my image?
    Last edited by surlyTomato; 08-22-2009 at 02:08 AM.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    13
    All fixed, thanks.
    Most of my problems were coming from an odd order of declaration thing (which I have yet to fix, but that's another problem).

    If anyone has any idea...
    Here
    Code:
    void couleurd(char* methode, char* refCam, int x, int y, int z_d){
    	
    	int i;
    	unsigned char *DataC;
    	int xSize, ySize;
    	imginfo *infoCoul;
    	int ret;
    	
    	//int i;
    //	int xSize, ySize;          ?????????????????????? seg fault if declared here??
    	//unsigned char *DataC;
    		
    	if(strcmp(methode,"plusproche")==0){
    		printf("refcam: %s, x: %d y: %d z:%d \n\n",refCam,x,y,z_d);
    		ret = LoadImage(refCam, infoCoul);
    		pixelC imPixels[100][100];
    		
    		DataC = infoCoul->Data;
    		printf("Data stuff: %d\n", DataC[4]);
    		//fillPixelArray(Data, imPixels);
    	}
    	return;
    }
    The output is
    Code:
    Image x and y size in pixels: 100 100
    Image zsize in channels: 3
    Image pixel min and max: 0 255
    Data stuff: 25
    Here:
    Code:
    void couleurd(char* methode, char* refCam, int x, int y, int z_d){
    	
    	//int i;
    	unsigned char *DataC;
    	int xSize, ySize;
    	imginfo *infoCoul;
    	int ret;
    	
    	int i;
    //	int xSize, ySize;          ?????????????????????? seg fault if declared here??
    	//unsigned char *DataC;
    		
    	if(strcmp(methode,"plusproche")==0){
    		printf("refcam: %s, x: %d y: %d z:%d \n\n",refCam,x,y,z_d);
    		ret = LoadImage(refCam, infoCoul);
    		//xSize = infoCoul->XSize;
    		//ySize = infoCoul->YSize;
    		//pixelC imPixels[xSize][ySize];	?????????????? seg fault!
    		pixelC imPixels[100][100];
    		
    		DataC = infoCoul->Data;
    		printf("Data stuff: %d\n", DataC[4]);
    		//fillPixelArray(Data, imPixels);
    	}
    	return;
    }
    I get a seg fault, when all I did was declare the i, after declaring ret.
    Anything that I declare after ret will give me a seg fault. I'm stumped.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by surlyTomato View Post
    If I comment the blah line out, it's fine. -- [edit: this part is fixed]

    Also, I'm sorry, but I don't see what you mean by "you do not get a string free with every struct", since Data is filled by LoadImage().

    Doesn't that mean that Data points to the pixel values in my image?
    The reason is that Data is an array of 64 bytes as allocated by malloc but for the system to know that it is a string it must be terminated with a null character '\0' which is not being done anywhere in your code and hence when you try to print it using %s it gives you an error.

    Also i dont see any reason why you should be getting a segfault just because you are declaring a variable after some place instead of at the beginning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM

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