Sollertis
Sollertis

Reputation: 15

C pointer to pointer warning

I get a warning when i point to a pointer. I am not sure why; the types are the same.

char delay_buffer1[40];
char delay_buffer2[40];
char** delay_buffer_front;
delay_buffer_front = &delay_buffer1;

the objective is to swap buffers:

void swap_string_buffer(void** pointer, void* buffer1, void* buffer2) {
  printf("pointer: %u. buffer1: %u. buffer2 %u\n", pointer, buffer1, buffer2);
  if(*pointer == buffer1) *pointer = buffer2;
  else *pointer = buffer1;
}

example code:

copy_content_to(delay_buffer_front);
swap_string_buffer((void**) delay_buffer_front, (void*) delay_buffer1,(void*)delay_buffer2);
fprintf(file, "%s", delay_buffer_front);

warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] delay_buffer_front = &delay_buffer1;

Upvotes: 0

Views: 172

Answers (3)

Shrikanth N
Shrikanth N

Reputation: 652

As all you want is to swap the buffers you dont have to create a pointer to a pointer. Simply create an intermediate pointer of similar type and use it for swapping.

char* delay_buffer_front; //Intermediate pointer to store the address of first buffer
delay_buffer_front = delay_buffer1; //Store the address of the first buffer in the 
                                    //intermediate pointer.

Upvotes: 0

user2371524
user2371524

Reputation:

the types are the same.

No, they aren't. You're probably confused (this is quite common) by the fact, that the identifier of an array evaluates to a pointer to its first element in nearly all contexts (exceptions are e.g. with the operators & and sizeof).

But that doesn't change the fact that an array is an array and a pointer is a pointer.

This is a pointer to a pointer:

char **delay_buffer_front;

What you probably wanted was a pointer to an array:

char (*delay_buffer_front)[40];

Or you might simply have wanted a plain pointer pointing to the first element of the array:

char *delay_buffer_front;

With that declaration, you can just assign the array's identifier because as explained above, it evaluates to such a pointer:

char *delay_buffer_front = delay_buffer1;

Upvotes: 2

Jabberwocky
Jabberwocky

Reputation: 50831

It means exactly that, the two pointers are incompatible. It's more an error than a warning.

You probably want this:

char* delay_buffer_front;
delay_buffer_front = delay_buffer1;

instead of:

char** delay_buffer_front;
delay_buffer_front = &delay_buffer1;

Upvotes: 1

Related Questions