top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the use of forward declaration in C, please explain with example?

0 votes
411 views
What is the use of forward declaration in C, please explain with example?
posted Feb 13, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Forward declaration is a kind of incomplete definition where compiler does know the memory layout of the declared variable but it can use this variable as a pointer since pointer size is same for all the variables. One more advantage is all the functions which are used in the program but defined somewhere else, declared first in the program.
Example:

struct X; // forward declaration

void f(struct X*) { } // usage of the declared, undefined structure

// void f(struct X) { } // ILLEGAL
// struct X x; // ILLEGAL
// int n =sizeof(struct X); // ILLEGAL

// later, or somewhere else altogether
struct X { /* ... */ };

answer Feb 14, 2017 by Harshita
...