Forwards declaration question?

This is a discussion on Forwards declaration question? within the C Programming forums, part of the General Programming Boards category; Hello, I have recently looked into forwards declarations and what they do. I understand how to code forward declarations but ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    45

    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 04:32 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Rec.h will have to include pc.h so that your pc struct is visible to the int REC_match_link_id() function. Frankly I'd just combine the two headers.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    Quote Originally Posted by see the big C View Post
    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?
    You need to look more closely at your own code. REC.h does not #include PC.h. PC.h declares the pc type after #include'ing REC.h.

    Therefore, when reading REC.h, the compiler has no information about what the pc type is (typedefs that come later as you - or the compiler - reads the file don't count).

    That means REC.h never has any information about what the type pc is. Since REC.h declares a function that accepts a pointer to pc, but the type pc has not been defined at any point when REC.h is included, a forward declaration is required.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    Hi CommonTater,

    >Rec.h will have to include pc.h so that your pc struct is visible to the int REC_match_link_id() >function.

    Oh yeah well that's what I thought! try it.. you'll see it still doesn't work!

    Rob

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    Excellent Grumpy,

    I see it now.

    Thanks!

    Sincere regards
    Rob

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21