An address of a pointer as opposed to a pointer to a pointer?

EDIT@22:14 I take it back. The data was getting borked on exit from the function. Turns out that the code in use had RETURN() as a macro which freed the pointer I was using when I called RETURN(TRUE). Haha…. still doesn’t answer my original question below though.
/EDIT

I thought they were essentially the same. A pointer to a pointer is the address of an address and surely the address of a pointer is an address of an address? I’ve clearly forgotten something fundamental here so on the off chance somebody can help. I’ve done this a few times now and fixed it without really thinking what I’m doing. This time however it caught me out. For the whole of today I’ve been fiddling with one routine only to find a final problem when I’d hashed out the algorithm to use. I have this defined as a function:


bool read_bitmap_file_data(unsigned char *cstream, unsigned int *width, unsigned int *height, unsigned char **data);

It used to take a file handle but it’s easier to bung it a stream of characters as variable cstream. data is what I receive back. A pointer to a pointer. Within the function it goes through and parses the stream adding bits to data as follows:


unsigned char *ptr;
int bytes;

for (bytes = 0, ptr = bits; bytes < size; bytes++, ptr++) { if ((value = next_int (&p_cstream)) < 0) RETURN (FALSE); *ptr=value; }

The above snippet is actually stolen from an old file called xbm.c (google it). After that loop it does this assignment if bits has data:


*data = bits;

If I define the variable passed to it as a pointer to a pointer like so:


unsigned char **hex_sudoku;
read_bitmap_file_data(xbm_sudoku,&width,&height,hex_sudoku);

It segfaults on the assignment mentioned above. If I declare the parameter and call the function like so:


unsigned char *hex_sudoku;
read_bitmap_file_data(xbm_sudoku,&width,&height,&hex_sudoku);

it is fine. In both instances the contents of the variable bits is identical so it is something about declaring a pointer to a pointer and just passing it as opposed to declaring a pointer and passing the address. What am I actually passing in the first instance if not an address?

Ah well... it all works now and I have not exactly wasted my time here.

This entry was posted in Programming and tagged , . Bookmark the permalink.