Confusion about how pointers are declared in C

The syntax commonly used in C to initialize pointers is

int *p = &x;

      

where p

is our pointer, and x

is the previously declared variable. I unfortunately do not understand this syntax: *p

not equal &x

, but equal instead x

. So why not the correct syntax

int *p = x;?

      

Does the compiler just make an exception to the usual variable assignment rules when dealing with pointers?

+3


source to share


6 answers


The C declaration syntax is admittedly confusing. As a rule, the rule follows that "declaration follows use", but this is not a hard and fast rule.

The type name int*

means "pointer to int

", but to define an object of p

type "pointer to int

" we don't just write this type name followed by the name of the object:

int* p;

      

Instead, we write:

int *p;

      

which means it *p

has a type int

; it follows that there p

should be a type int*

.

You've probably noticed that both declarations are the same except for the spacing - and the compiler doesn't care about the distance. In fact, some programmers prefer to write int* p;

, although this is strictly against the grammar, because it is clearer in that case.

The distinction becomes important if you want to define multiple objects in a single declaration:

int *p, *q;

      



means that *p

and *q

are types int

. AND

int *x, y;

      

means that *x

and y

have a type int

- this means that it x

is int*

and y

is int

. Programmers who prefer to place *

next to int

should split this into two lines (which is a good idea anyway).

So, to declare an object, the extra specifiers are associated with the name being defined, not the type. But the initializer is applied to the newly created object. So:

int *p;

      

means it *p

has a type int

, but this:

int *p = &x;

      

means it is p

initialized with a value &x

.

I won't try to argue that this is how the C declaration syntax was supposed to be defined. It has its own consistent logic to it, but it caused a lot of confusion. But that's the way it is.

+2


source


read it like this

int* p = &x;

      



ie int * is a type - a pointer to int;

+3


source


The symbol *

has a different meaning depending on where you find it. I can think of three meanings off my head:

1. In the ad

int *x;

      

Here we are writing a variable declaration. We create a variable named x

with a type int *

.

2. As an expression dereference

int *x = malloc(sizeof(int));
int y = *x;

      

Here we are using the same token, but note that it is now inside an expression! (The right side of the equal sign is an expression.) Here it means dereferencing.

3. How multiplication

int *x = malloc(sizeof(int));
int y = *x * *x;

      

Here we use the same symbol for multiplication!

So what gives?

The compiler can use the context around the symbol *

to determine which of the three cases we are in. Of course, we must have different symbols to represent these three things, but this is not the language we are in.

+2


source


Coming back to your declaration.

int *p = &x;

      

Let's take the first half. "Int * p" tells the compiler that the variable "p" exists in memory as a value and should be interpreted as "int". The second half, '& x', means to get me a place in the memory of the variable x. Thus, you can store the "x memory location" in a variable that represents the "memory location".

The analogy in the real world would be if you had houses on the street, each with an address (one of the houses would be "x" in your example), and you also have a "p" card on which you can write the address of the house ... You can't put a house, x, on a postcard. You can only put the address of the house, & x, on the postcard.

+1


source


int *p = &x;

When prefixed *

in a declaration, the identifier is defined as a pointer to the given type. For example, p

is a pointer to x

; Read it this way:

  • declare a pointer to int

    calledp

  • to the pointer p

    , assign the following value:the address of x

If we want to use p

to get a value x

, we do what is called dereferencing:*p

For example:

int x = 35;
int *p = &x;
printf("x = %d\n", *p); // Prints x = 35
printf("%p\n", p); // Prints the address of x (which is the same as the value of p)
printf("%p\n", &p); // Prints the address of the variable p

      

0


source


int x = 5; // Declare integer 'x' and initialize it with 5.

int * p1 = & x; // Declaring a pointer 'p1' and initializing it with address 'x'.

printf ("x =% d \ n", * p1); // Send the 'p1' pointer to get the 'x' value.

int * p2 = NULL; // Declaring a pointer to int. Initialize it to NULL.

p2 = & x; // Assign address 'x' to pointer 'p2'.

printf ("x =% d \ n", * p2); // Reject the 'p2' pointer to get the value 'x'.

0


source







All Articles