-2

Possible Duplicate:
What is forward reference in C?

I read this question somewhere

"What is forward reference with respect to pointers in C, and what are its advantages"

The accepted answer mentioned in here : What is forward reference in C? doesn't give a proper explanation? Can anyone explain what exactly it means? and does it have any advantages?

Community
  • 1
  • 1
SandBag_1996
  • 1,570
  • 3
  • 20
  • 50

1 Answers1

2

Forward Referencing wrt Pointers is the term we use, when

A pointer is declared and compiler reserves the memory for the pointer but the DataItem is not defined to which the pointer points to.

From the previous answers::

struct MyStruct *ptr; // this is a forward reference cuz pointer ptr is declared
                      // but the structure it points to is itself not defined till now.

struct MyStruct // Now the structure is declared
{
       // some data members
};

Advantages:: Advantages

Abhineet
  • 5,320
  • 1
  • 25
  • 43
  • In this struct MyStruct; struct MyStruct *ptr; struct MyStruct var; // ILLEGAL why is var illegal? – SandBag_1996 Feb 02 '13 at 07:29
  • 2
    See the point is, compiler needs to know the size of "struct MyStruct" to reserve memory for "var" but "ptr" is a "pointer" which will only store the address, thus to reserve memory of "ptr", compiler already knows the amount of memory to allocate. – Abhineet Feb 02 '13 at 07:33