Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to a structure that has not been declared [duplicate]

Tags:

c

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?

like image 432
Bazooka Avatar asked Feb 05 '12 09:02

Bazooka


People also ask

How to declare a structure pointer in C?

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;

Can We have a pointer as a member of a structure?

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.

What is a pointer in C++?

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.

How to initialize a structure pointer?

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:


1 Answers

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

like image 106
dreamlax Avatar answered Nov 02 '22 20:11

dreamlax