What is forward reference in C?
Asked Answered
L

6

6

What is forward reference in C with respect to pointers?

Can I get an example?

Livery answered 28/11, 2008 at 16:52 Comment(0)
R
11

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.

Romilly answered 28/11, 2008 at 16:54 Comment(2)
ok thank u but can i assume that there is no difference with pointers in thsi caseLivery
Yes: pointers variables are variables, as are integer variables.Romilly
G
9

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.

Guesswork answered 28/11, 2008 at 17:22 Comment(0)
P
5

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 "PlopUtils.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 ...
}
Predicant answered 28/11, 2008 at 17:45 Comment(0)
U
3

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.

Unquiet answered 28/11, 2008 at 20:36 Comment(0)
E
3

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;
};
Ephemerid answered 29/11, 2008 at 1:19 Comment(0)
D
-5

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.

Destined answered 28/11, 2008 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.