views:

1966

answers:

6

What is forward reference in C with respect to pointers?

Can I get an example?

+8  A: 

See this page on forward references. I don't see how forward referencing would be different with pointers and with other PoD types.

Note that you can forward declare types, and declare variables which are pointers to that type:

struct MyStruct;
struct MyStruct *ptr;
struct MyStruct var;  // ILLEGAL
ptr->member;  // ILLEGAL

struct MyStruct {
    // ...
};

// Or:

typedef struct MyStruct MyStruct;
MyStruct *ptr;
MyStruct var;  // ILLEGAL
ptr->member;  // ILLEGAL

struct MyStruct {
    // ...
};

I think this is what you're asking for when dealing with pointers and forward declaration.

strager
ok thank u but can i assume that there is no difference with pointers in thsi case
Manoj Doubts
Yes: pointers variables are variables, as are integer variables.
strager
+6  A: 

I think "forward reference" with respect to pointers means something like this:

struct MyStruct *ptr; // this is a forward reference.

struct MyStruct
{
  struct MyStruct *next; // another forward reference - this is much more useful
  // some data members
};

The pointer is declared before the structure it points to is defined.

The compiler can get away with this because the pointer stores an address, and you don't need to know what is at that address to reserve the memory for the pointer.

Arkadiy
+2  A: 

Forward reference is when you declare a type but do not define it.

It allows you to use the type by pointer (or reference for C++) but you cannot declare a variable.

This is a way to say to the compiler that something exists

Say that you have a Plop structure defined in Plop.h:

struct Plop
{
   int n;
   float f;
};

Now you want to add some utility functions that works with that struct. You create another file PlopUtils.h (let's say you can't change Plop.h):

struct Plop; // Instead of including Plop.h, just use a forward declaration to speed up compile time

void doSomething(Plop* plop);
void doNothing(Plop* plop);

Now when you implement those function, you will need the structure definition, so you need to include the Plop.h file in your PlopUtils.cpp:

#include "PlopHelper.h"
#include "Plop.h" // now we need to include the header in order to work with the type

void doSomething(Plop* plop)
{
   plop->n ...
}

void doNothing(Plop* plop);
{
   plop->f ...
}
Vincent Robert
A: 

Forward references allow C compiler to do less passes and significantly reduces compilation time. It is probably was important some 20 years ago when computers was much slower and compliers less efficient.

Din
+1  A: 

I think the C compiler originally had a pass in which it did symbol table building and semantic analysis together. So for example:

    ....
    ... foo(a,b) + 1 ... // assumes foo returns int
    ....

    double foo(double x, double y){ ... } // violates earlier assumption

to prevent this, you say:

    double foo(double x, double y); // this is the forward declaration

    ....
    ... foo(a,b) + 1 ... // correct assumptions made
    ....

    double foo(double x, double y){ ... } // this is the real declaration

Pascal had the same concept.

Mike Dunlavey
+1  A: 

Adding to previous answers. The typical situation in which forward reference is mandatory is when a struct foo contains a pointer to a struct bar, and bar contains a pointer to foo (a circular dependency between declarations). The only way to express this situation in C is to use a forward declaration, i.e.:

struct foo;

struct bar
{
   struct foo *f;
};

struct foo
{
   struct bar *b;
};
Federico Ramponi