Help with nested struct pointers

This is a discussion on Help with nested struct pointers within the C Programming forums, part of the General Programming Boards category; I am tasked with writitng a program that populates a data file based on the contents of structures defined in ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    6

    Help with nested struct pointers

    I am tasked with writitng a program that populates a data file based on the contents of structures defined in a library. Below are the structures.
    Code:
    struct simrad_struct
    {
    int num_records;
    ...
    struct simrad_record_struct * recs;
    }
    
    struct simrad_record_struct
    {
    long id;
    ....
    long office_num
    }
    I cannot change these structures to typedefs since they are in a library.

    My program has a function which takes in a populated simrad struct:
    Code:
    int (char * sim)
    {
    struct simrad_struct * sim_pop;
    sim2_pop = (struct simrad_struct *) sim;
    
    .....
    return 0;
    }
    what I need to do is access the value of office_num, in the nested struct.
    Code:
    long office =  sim2_pop->recs->office_num;
    gives the error "dereferencing pointer to incomplete type".

    Does anyone know how I can access this value?

    note that the following code works.
    Code:
    int num = sim2_pop->num_records;

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Looks like the simrad_record_struct type is incomplete at the time you attempt to access the ->rec member. Meaning it's been forward-declared but never defined. Make sure you're including all the relevant header files (in your code, as well as in this discussion)

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    I have the file where the sctructs are defined as an include statement.

    #include simrad.h

    and the referenced in the makefile.

    I even tried the following. The first line compiles but the second has the same isseu as before
    Code:
    struct simrad_record_struct * temp = sim2_pop->recs;
    long office = temp->office_num;
    Last edited by vampireiam; 11-09-2007 at 12:03 PM. Reason: wanted to add another code section

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Are you SURE that the definition of simrad_record_struct is in the file simrad.h? Is it possibly surrounded by an #if or #ifdef which is turning the definition off for some reason?

    Also I hope you have "" or <> around that include -- didn't show up in your post. Obviously, you need one of those.

    I even tried the following. The first line compiles but the second has the same isseu as before
    That proves that simrad_record_struct has been forward declared like this:

    Code:
    struct simrad_record_struct;
    But the actual definition (the members of the struct) is not available.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    I have the "" around the include just fogot to type it.

    I am lookingat the file and the structs are both in there. The file basically contains a series of structs of the form
    Code:
    struct <name>
    {
    }
    There are no conditional statements to turn the various structs on and off. Basically simrad_struct includes a pointer to each of the other structs as well as some standard members (int, long, etc...). They are all forward declared so I dont understand why I am access simrad_struct but not simrad_record_struct since they were both forward declared.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Except for the missing semicolons on the end of all the struct definitions (did you just forget to type them, too?) I see nothing wrong. Unless you can post the full header file I'm stumped.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    I am going to relook at the get_data function to see if I can figure out how they loaded the simrad_struct itself. In the meanwhile here is a more description post of the code.

    Code:
    simrad.h
    ------------------
    
    struct simrad_struct
    	{
    	/* type of data record */
    	int	kind;		
    	int	type;		
    
    	/* pointer to recod data structure */
    	struct simrad_record_struct *record;
    	};
    
    struct simrad_record_struct
    	{
    	int	png_date;	
    	int	png_msec;	
    	int	png_count;	
    	int	png_serial;	
    	int	png_latitude;	
    	int	png_longitude;	
    
            ..... a bunch of ints and longs that define teh record ......... 
    	};
    
    
    
    main.c
    ---------------------
    char 	*istore_ptr;     //generic data from file
    int status;
    ......
    //read the data
    status = get_data((void **) &istore_ptr);   
    ......switch statement.......
    //store the data
    status = store_data(istore_ptr);
    
    
    simrad2.c
    ----------------------
    #include "simrad.h"
    .......other includes...............
    
    int sim22gsf(char *sim)
    {
    struct simrad_struct * sim2_pop;
    sim2_pop = (struct simrad_struct *) sim;
    
    //these work
    int type = sim2_pop->type;
    int kind = sim2_pop->kind; 
    
    //now we need to get the actual record data
    int date = sime2_pop->record.png_date;     // this gets the error mentioned.
    
    .......store the data in the new format..........
    return 0;
    }
    Last edited by vampireiam; 11-09-2007 at 12:54 PM. Reason: error in code

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    int date = sime2_pop->record->png_date;
    Kurt

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by vampireiam View Post
    int date = sime2_pop->record.png_date; // this gets the error mentioned.
    This should be:

    Code:
    int date = sim2_pop->record->png_date;

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    Me and my stupid typos.

    I meant that
    Code:
    int date = sim2_pop->record->png_date;   //does not work

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    I have verified that it isnt how the structs are declared or how I am attepmting to access the field in the nested struct. I did a simple example and got everything to work using the structs as defined above. There are some other declerations in the .h file that appear to be causing the issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM
  4. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 06:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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