Hello,
I have recently looked into forwards declarations and what they do. I understand how to code forward declarations but I don't understand why we would need them in this particular example.
Please view the small code sample and I will pose my question at the end:
Code:
=================mainc. (does nothing)
#include <stdio.h>
#include "PC.h"
#include "REC.h"
int main()
{
int i;
}
==================PC.h
#ifndef PC_H
#define PC_H
#include "REC.h"
typedef struct tag_pc
{unsigned short gso_flash_address;
} pc;
#endif // PC_H //
==================PC.c
#include <stdio.h>
#include "PC.h"
// Methods here...
==================REC.h
#ifndef REC_H
#define REC_H
//struct tag_pc; // Forwards declaration commented on purpose
typedef struct tag_rec_data
{unsigned short LKdct__F1;
}rec_data;
typedef struct tag_rec
{unsigned short f__pc_xchoice;
rec_data *records;
}rec;
int REC_match_link_id(pc *pObj);
// struct tag_pc *pObj);
#endif // REC_H //
===================REC.c
#include <stdio.h>
#include "PC.h"
#include "REC.h"
int REC_match_link_id(pc *pObj)
//struct tag_pc *pObj)
{
// ... other code
}
========================
Okay, the above code gives an error at the "int REC_match_link_id(....)" function declaration and implementation levels. My question is:
If REC.c includes REC.h and REC.h include PC.h (which is where pc is declared), why is it still not sufficient for the compiler to recognize the "pc *pObj" parameter in the int REC_match_link_id(....) function parameter list?
I mean isn't the whole PC.h header included in the REC.c compiler's translation unit???? Then, why is it that I have use a forwards declaration for the code above to compile correctly??? I know that if I uncomment the forwards declaration in REC.h and replace the "pc *pObj" parameter to "struct tag_pc" in the function's parameter list everything will compile... but I don't see why it wouldn't compile if I leave the code as above!
Basically, when we include the PC.h in the REC.h, then, if pc is used anywhere in REC.h/.c it should have visible scope... no?
All feedback sincerely appreciated!