-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?

1
  • I guess I mentioned that in question.. please care enough to read the question before downvoting Feb 2, 2013 at 7:16

1 Answer 1

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

2
  • In this struct MyStruct; struct MyStruct *ptr; struct MyStruct var; // ILLEGAL why is var illegal? Feb 2, 2013 at 7: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 2, 2013 at 7:33

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