incompatible pointer type

This is a discussion on incompatible pointer type within the C Programming forums, part of the General Programming Boards category; I am trying to parse the information in a TIFF file. To do this, I have an array of unsigned ...

  1. #1
    Lode Runner
    Join Date
    May 2004
    Posts
    53

    incompatible pointer type

    I am trying to parse the information in a TIFF file. To do this, I have an array of unsigned bytes.

    Code:
    FILE * tiffFile;
    int i,j;
    GLubyte (* fields)[12];
    unsigned short numOfFields=0;
    long index;
    
    ...
    //get number of fields
    ...
    
    fields = malloc(numOfFields*12*sizeof(GLubyte));
    
    for(i=0;i<numOfFields;i++) {
    	for(j=0;j<12;j++) {
    		fread(&(fields[i][j]),1,1,tiffFile);
    	}
    }
    
    index = findTag(0x100,fields,numOfFields);
    The findTag function is as follows.

    Code:
    long findTag(short tag, GLubyte **fields, int numOfFields){
    	int i,found=0;
    	for(i=0;(i<numOfFields) && !found;i++){
    		if (tag == fields[i][0]*256 + fields[i][1])
    			found = 1;
    	}
    	printf("Index of tag: %x is: %d",tag,i);
    	return i;
    }
    For a reason I do not understand, I get a warning "passing arg 2 of 'findTag' from incompatible pointer type". And while executing the findTag function, I get a Bus Error, usually (but not always) when i=1.
    I checked in the debugger, fields in the main function, or in findTag points to the same memory address. And I don't understand why it works perfectly when I fill it up in the main, but not when I want to read from it in findTag.
    I tried to use a fixed size for the array, but I get the exact same problem.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) This is not a pointer to a pointer:
    Code:
    GLubyte (* fields)[12];
    2) This is:
    Code:
    long findTag(short tag, GLubyte **fields, int numOfFields){
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    Understood. Though I always thought arrays were handled as pointers. Anyway, what type do I declare as argument 2 of findTag ? Or any other way of passing my arrays to findTag.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You should be okay if you just drop the parentheses around the fields declaration. GLubyte (* fields)[12] is not the same as GLubyte *fields[12]. In the latter, the * is associated with the GLubyte instead of fields.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    This is the way I want it to be : a GLubyte *fields[12] does not work in this case (segfault while filling it up).

    For the sake of facilitating the example, suppose I declare :
    Code:
    GLubyte fields[65536][12]
    I still get "passing arg 2 of 'findTag' from incompatible pointer type". If I want to pass this double array to my function, how do I declare it ?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by krappa
    This is the way I want it to be : a GLubyte *fields[12] does not work in this case (segfault while filling it up).
    Do you actually allocate any space for all of those pointers, or are you just trying to use them all uninitialized?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    I initialize it right here :
    Code:
    //get number of fields
    ...
    fields = malloc(numOfFields*12*sizeof(GLubyte));
    I've excluded the details of getting numOfFields, but it works, I've checked in the debugger.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well that's part of your problem. You're doing it wrong.
    Code:
    fields = malloc( sizeof( GLubyte * ) * 12 );
    for( x = 0; x < 12; x++ )
        fields[ x ] = malloc( sizeof( thisdataelement ) * lengthofthisarrayrow );
    When you're done, you have to free it in the reverse:

    Code:
    for( x = 0; x < 12; x++ )
        free( fields[ x ] );
    free( x );

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    Perhaps I wasn't very clear, but you didn't exactly understand what I wanted.
    Anyway, I solved the problem by just declaring arg 2 of findTag as
    Code:
    long findTag(short tag, GLubyte (* fields)[12], int numOfFields)
    I just thought I could make it work without putting the exact same type and thus, not forcing the array size.

    Thanks for the help, anyway, guys.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    > GLubyte (* fields)[12];
    > fields = malloc(numOfFields*12*sizeof(GLubyte));
    Of course, you should be doing this.
    fields = malloc ( numOfFields * sizeof *fields );

    See http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Which coupled with
    long findTag(short tag, GLubyte (* fields)[12], int numOfFields)
    Is just fine.

    Indeed, you could go on to say
    GLubyte foobar[100][12]; // major size doesn't matter, so long as minor size is 12
    And then call your function with
    findTag(0, foobar, 100);
    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. warning: assignment from a incompatible pointer type
    By enderandrew in forum C Programming
    Replies: 8
    Last Post: 09-22-2007, 05:07 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

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