Deepak
Deepak

Reputation: 790

Declaring a pointer to a structure

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?

Upvotes: 0

Views: 293

Answers (2)

Ashkan Kahbasi
Ashkan Kahbasi

Reputation: 69

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".

Upvotes: 1

Bill Morgan
Bill Morgan

Reputation: 570

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.

Upvotes: 1

Related Questions