Thread: Is there a way to point to one structure array entry from another at compile time?

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    Is there a way to point to one structure array entry from another at compile time?

    Here's the scenario.

    I have some structures...

    Code:
    struct entry { 
         char name[8] ;   
         int  age  ;  
    } ; 
    
    struct entry Table1[] = {     // An array of entry's. 
         "Dino"   , 1 , 
         "Bambam" , 2 , 
         "Pebbles", 3 
    } ;  
    
    struct section { 
         struct entry * an_entry ;  
         int  length ;  
         char data[8] ;  
    } 
    
    // I'm struggling with the structure below... 
    
    struct section Table2[] = {   // An array of sections 
        & first entry in Table1  , 5, "abc" ,  
        & second entry in Table1 , 5, "def" ,  
        & third entry in Table1  , 2, "9999"  
    } ;
    Conceptually, I would like to be able to assign a label to each entry of Table1 so that Table2 section.an_entry points to the Table1 entry I want. It's a constant table and does not change at runtime.

    This, obviously isn't legal syntax, but it would look like this

    Code:
     
    struct entry Table1[] = {     // An array of entry's. 
    e1:    "Dino"   , 1 , 
    e2:    "Bambam" , 2 , 
    e3:    "Pebbles", 3 
    } ;  
    
    struct section Table2[] = {   // An array of sections 
        &e1 , 5, "abc" ,  
        &e2 , 5, "def" ,  
        &e3 , 2, "9999"  
    } ;
    Is there a way to do this? I haven't tried to see if it's legal, but if it is, I would rather not code
    Code:
     
    struct section Table2[] = {   // An array of sections 
        &Table1[0] , 5, "abc" ,  
        &Table2[1] , 5, "def" ,  
        &Table1[2] , 2, "9999"  
    } ;
    because Table1 might change in the future, and it might have 50 elements in it. I would prefer a symbolic label or address instead of hardcoding an array subscript (again, if it's even legal).
    Last edited by Dino; 09-01-2022 at 05:56 PM.
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So long as your compiler is at least C99, you can use designated initialisers.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    enum {
        E_DINO,
        E_BAMBAM,
        E_PEBBLES,
    };
    
    struct entry { 
         char name[8] ;   
         int  age  ;  
    } ; 
     
    struct entry Table1[] = {     // An array of entry's. 
         [E_DINO] = { "Dino"   , 1 }, 
         [E_BAMBAM] = { "Bambam" , 2 }, 
         [E_PEBBLES] = { "Pebbles", 3 }
    } ;  
     
    struct section { 
         struct entry * an_entry ;  
         int  length ;  
         char data[8] ;  
    } ;
     
    // I'm struggling with the structure below... 
     
    struct section Table2[] = {   // An array of sections 
        { &Table1[E_DINO], 5, "abc" },  
        { &Table1[E_BAMBAM], 5, "def" },  
        { &Table1[E_PEBBLES], 2, "9999"}
    } ;
    
    int main ( ) {
        printf("%s\n", Table2[1].an_entry->name);
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Works perfect! Great solution. Thank you!
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entry Point Not Found?!
    By pobri19 in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2008, 09:00 AM
  2. C# COM entry point
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-14-2007, 01:51 AM
  3. Program Entry Point
    By Sentral in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2006, 03:36 AM
  4. entry point of MFC Application
    By Laeeqhamid in forum Windows Programming
    Replies: 1
    Last Post: 12-26-2002, 09:06 AM
  5. What a function entry point?
    By KoolGuy18 in forum C Programming
    Replies: 1
    Last Post: 11-01-2002, 11:29 PM

Tags for this Thread