Thread: Forwards declaration question?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    53

    Forwards declaration question?

    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!
    Last edited by see the big C; 12-04-2010 at 05:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about a declaration
    By Boxknife in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 10:28 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. errors initializing D3D
    By Vacation Guy in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2005, 12:20 PM