Possible Duplicate:
undefined C struct forward declaration
How is it possible to declare a pointer to structure even when I do not declare a structure?
#include<stdio.h>
int main(){
struct s{
struct p *ptr;
};
}
Why does the above compile successfully?
To declare a pointer variable in C, we use the asterisk (*) symbol before the variable's name. After defining the structure pointer, we need to initialize it, as the code is shown: ptr = &structure_variable; We can also initialize a Structure Pointer directly during the declaration of a pointer. struct structure_name *ptr = &structure_variable;
We can also have a pointer as a member of the structure. For example: Here ptr_mem is a pointer to int and a member of structure test. There are two ways in which we can access the value (i.e address) of ptr_mem: Similarly, there are two ways in which we can access the value pointed to by ptr_mem.
We have already learned that a pointer is a variable which points to the address of another variable of any data type like int, char, float etc. Similarly, we can have a pointer to structures, where a pointer variable can point to the address of a structure variable.
We can also initialize a Structure Pointer directly during the declaration of a pointer. struct structure_name *ptr = &structure_variable; As we can see, a pointer ptr is pointing to the address structure_variable of the Structure. There are two ways to access the member of the structure using Structure pointer:
It's possible because the compiler doesn't need to know anything about the structure if it only deals with a pointer to it.
This is a commonly used technique and is usually called an “opaque pointer”.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With