8

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?

1

2 Answers 2

10

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

6
  • can you please link me to a detailed explanation ?
    – Bazooka
    Feb 5, 2012 at 9:42
  • 2
    @Parminder There's a section called "Self-referential Structures" in The C Programming Language.
    – cnicutar
    Feb 5, 2012 at 9:43
  • 3
    Effectively, the struct p line is a declaration. Feb 5, 2012 at 9:48
  • @dreamlax: I think good explanation by you... But a new thing I learnt today is Opaque Pointer... Anyhow Thanks Feb 5, 2012 at 10:13
  • 1
    @RasmiRanjanNayak True but that section explains why this is possible (forward declaration stuff).
    – cnicutar
    Feb 5, 2012 at 10:17
0

Look into the below link

http://cboard.cprogramming.com/cplusplus-programming/100298-opaque-pointer.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.