what is the difference between pointer to structure and structure pointer

Status
Not open for further replies.

chandu.kurapati

Full Member level 3
Joined
Oct 31, 2013
Messages
186
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Location
Bengaluru, India
Activity points
2,778
Hi every one,
i was writing some program like this

typedef struct
{
int a;
int b;
}ST;

ST *q; // structure pointer

ST* p; // Pointer to structure

what are the difference between the structure pointer & pointer to structure and what are the advantages of both, please explain me?


Thanks & Regards,

Chandu.Kurapati.
 

The answer is rather trivial, the C syntax parser doesn't care for spaces in the declaration, these three declarations have the same meaning.
ST *q;
ST* q;
ST*q;
 

Hi FVm,
Please try this,

typedef struct
{
int a;
int b;
}ST;

ST* p;

int main()
{
p->a = 1;
p->b = 2;
printf("%d %d "p->a,p->b);
}

what is the result and why it is like that, explain me?

Thanks & Regards,
Chandu.Kurapati.
 

what is the result and why it is like that, explain me?
The result is unpredictable because p isn't initialized with a valid data memory address and no memory has been allocated for the data.

In your example p is a global static variable and will be initialized with 0. The result depends on the memory layout of the respective processor and if it implements an address error trap.
 

Pointer is a variable which holds the address of another variable of its type and points to the variable and can be used to read and write to that variable by dereferencing. Structs are data structures. Pointer to structure is a pointer which holds the address of a structure.
 

Hi FVm,
Please try this, it is working

typedef struct
{
int a;
int b;
}ST;

ST *q;

ST* p = &q;

int main()
{
p->a = 1;
p->b = 2;
printf("%d %d "p->a,p->b);
}

what is the difference between first one i posted and this post result?

I read about this like this way:

ST* p; //pointer to the structure


ST *q;// is a structure pointer.

what is the difference between the pointer to structure and structure pointer?Please explain me.

Thanks & Regards,
Chandu.Kurapati.
 

Maybe it is ST** p = &q; which is a pointer to a pointer to structure or pointer to a structure pointer.
 

Please try this, it is working
A good C compiler gives a warning about incompatible pointer assignment for the second line:
Code:
ST *q;

ST* p = &q;

In contrast to your first code snippet the pointer is now pointing to a valid memory address, but it's the address of pointer q, still no ST object created. Depending on the size of both objects, an assignment to p->b is possibly writing out of bounds. Reasonable code could look like:
Code:
ST st;

ST* p = &st;
what is the difference between the pointer to structure and structure pointer?
Please try to understand the essence of post #2. Both pointers have identical type. Neither I see a different meaning of both terms.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…