Question

I’m not good at pointer.

This is a sample program of pointer

int sample(){
        char* a;
        char* b;
        char* *c;
        a = &*c;
        b = &*c;
        *c = "abcd";
        printf("a = %s\n",a);
        printf("b = %s\n",b);
        printf("c = %s\n",*c);
        return 0;
}

There is a warning when I compile it.

warning: assignment from incompatible pointer type

The result is below.

a = 
b = abcd
c = abcd

Does anyone tell me why a doesn’t show any value while b shows same value of c?

And how can a show same value of c?

Was it helpful?

Solution

char *a;

The above statements define a to be a pointer to a character, i.e., it can store the address of a character.

char **c;

The above statement defines c to be a pointer to a pointer to a character, i.e., it can store the address of a pointer to a character type, i.e., char * variable.

a = &*c;
// equivalent to
a = &(*c)
// equivalent to
a = c;

c is of type char **, where as a is of type char * - a different type. These types are incompatible types which explains the warning message -

warning: assignment from incompatible pointer type

The variable c is unitialized which means it contains garbage value, i.e., its value is indeterminate.

*c = "abcd";

The above statement means that a pointer to the first character of the string literal "abcd" is stored in the location pointed to by c, but the value of c is garbage. It is undefined behaviour to dereference an uninitialized pointer. Undefined behaviour means unpredictable behaviour. Even though it may seem to work, anything can happen from program crash to your hard disk getting formatted. As such, there is no point in reasoning about code which invokes undefined behaviour. The standard imposes no requirement on the implementation to handle such cases and you should always avoid them.

Read these -

  1. Common undefined behaviour
  2. Undefined behaviour and sequence points

OTHER TIPS

a is an uninitialised variable with no value.

b is an uninitialised variable with no value.

c is an uninitialised variable with no value.

There is a special rule in C for &*: If it is syntactically correct, then &* performs no operation. So &*c is the same as c. The type of c is not the same as the type of a, so the assignment is illegal. Same for the assignment to b. Good compilers or compilers with the right settings refuse to compile instead of giving you a warning.

*c = "abcd": Since c is an uninitialised variable, assigning to *c is undefined behaviour. If you're lucky, your program crashes. If you're not lucky, it works on your development machine, and crashes and causes significant damage when a customer uses the software.

Your code has undefined behavior. You're not reserving any room for the pointer that c is pointing at, yet accessing it.

You assign a, which is a pointer of char, to c, which is a pointer of pointer of char. Different levels of pointers here, so is your warning. Taking &*c is the same as c in C.

So you try to read a string at address c and NOT at address *c (which is the actual address of "abcd") when you execute the line

printf("a = %s\n",a);

First of all, a = &*c and b = &*c is illegal pointer conversion, so you probably want a = *c. Second, you cannot assing *c to a before you even initialize it. So, this is, for example, legal:

*c = "abcd";
a = *c;
b = *c;

This is also legal:

char **a = c;
*c = "abcd";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top