Home > Mobile >  Declaring a pointer to a structure
Declaring a pointer to a structure

Time:03-20

What's the difference between

struct point var, *varptr; 

and

struct point *var;

Can we just use the second one without assigning any variable's address to it?

CodePudding user response:

The difference is that this statement creates two variables:

struct point var, *varptr;

It has the same effect as using 2 statements:

struct point var; // type struct point
struct point *varptr; // type pointer to struct point

And this statement creates 1 variable:

struct point *var; // type pointer to struct point

Can we just use the second one without assigning any variable's address to it?

Yes you can use the second one but you would want to initialize it to something (usually another variable's address) before derefrencing the pointer.

CodePudding user response:

In the first example var is a variable with the data type of struct point in the memory and *varptr is a variable that points to an ADDRESS in the memory with the data type of "struct point".

In the second example *var is the same as *varptr in the first example. It POINTS to a memory location with the data type of "struct point".

  • Related