Thread: returning an array of pointers to structures

  1. #1
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51

    returning an array of pointers to structures

    This is mostely a problem ive made for myself. I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLIST 10
    
    struct ipList {
      int id;
      char name[ 35 ];
      char ip[ 15 ];
    };
    
    void getList( struct ipList *list[ ] );
    struct ipList *makeIP( void );
    void printList( struct ipList *list[ ] );
    
    int main( void ) { 
      struct ipList *theList[ MAXLIST ];
      
      getList( theList );
    
      printList( theList );
    
      return 0;
    }
    
    void getList( struct ipList *list[ ] ) {
      int i = 0;
      
      while ( i < MAXLIST ) {
        list[ i ] = makeIP( );
        list[ i ]->id = i;
        strcpy( ( list[ i ]->name ), "citibank.co.cx" );
        strcpy( ( list[ i ]->ip ), "209.249.147.15" );
        i++;
      }
    }
    
    struct ipList *makeIP( void ) {
      struct ipList *temp;
      temp = malloc( sizeof( struct ipList ) );
      return ( temp );
    }
    
    
    void printList( struct ipList *list[ ] ) {
      int i = 0;
      
      while ( i < MAXLIST ) {
        printf( "%s - %s\n", ( list[ i ]->name ), ( list[ i ]->ip ) );
        i++;
      }
    }
    Which works, however ive been trying to do this without passing parameters to getList( ), instead I've been trying to have getList( ) return the address to an array of pointers to structures. I can make the array, but seem to be unable to have it be returned.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I can make the array, but seem to be unable to have it be returned.
    How are you creating your array? Like this:
    Code:
    struct something * getList( void )
    {
        struct something *list[ SIZE ];
        ... do stuff ...
    
        return list;
    }
    That won't work because the list is a local variable. You could "fix" this by making it a static variable. Or you could dynamicly create the "array" and return it.
    Code:
    struct something * getList( void )
    {
        struct something ** list;
        int x;
        list = malloc ( SIZE * sizeof(struct something*) );
        for( x = 0; x < SIZE; x++ )
            list[x] = malloc( sizeof( struct something ) );
    
        ... do stuff ...
    
        return list;
    }
    There's probably another way or so to do this, but that should suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLIST 10
    
    struct ipList {
      int id;
      char *name;
      char *ip;
    };
    
    struct ipList *getList( void );
    void printList( struct ipList *list );
    
    int main( void ) { 
      struct ipList **theList;
      int i;
    
      theList = malloc( MAXLIST * sizeof( struct ipList ) );
    
      *theList = getList( );
    
      i = 0;
      while ( i < MAXLIST ) {
        printf( "%s - %s\n", ( theList[ i ]->name ), ( theList[ i ]->ip ) );
        i++;
      }
    
      return 0;
    }
    
    struct ipList *getList( void ) {
      struct ipList **list;
      int i;
      
      list = malloc( MAXLIST * sizeof( struct ipList ) );
      i = 0;
      while ( i < MAXLIST ) {
      	list[ i ] = malloc( sizeof( struct ipList ) );
        list[ i ]->id = i;
        list[ i ]->name = "citibank.co.cx";
        list[ i ]->ip = "209.249.147.15";
        i++;
      }
    
      return ( *list );
    }
    
    void printList( struct ipList *list ) {
      int i = 0;
      
      while ( i < MAXLIST ) {
        printf( "%s - %s\n", ( list[ i ].name ), ( list[ i ].ip ) );
        i++;
      }
    }
    I run into one of problems I had before when I was messing around trying to get it to work where it seems only the first address remains in sequence the others are dropped off. I essentialy get an invalid access in memory when the second struct in the array is called.
    Last edited by dharh; 02-05-2003 at 09:29 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    At a very quick glance, I'd guess this is wrong:
    >>list = malloc( MAXLIST * sizeof( struct ipList ) );
    I think you mean:
    >>list = malloc( MAXLIST * sizeof( struct ipList *) );
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    The problem is that you're trying to be fancy with the return value of getList(). You allocate memory to theList, then assign the one element that getList() returns. Instead, have getList() return a double pointer and don't worry about memory in main except for freeing when you're done :-) You have the same problem with printList(), you're trying to use a single pointer when you really want and need a double pointer
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLIST 10
    
    struct ipList {
      int id;
      char *name;
      char *ip;
    };
    
    struct ipList **getList( void );
    void printList( struct ipList **list );
    
    int main( void ) {
      struct ipList **theList;
      
      theList = getList( );
      printList( theList );
      
      return 0;
    }
    
    struct ipList **getList( void ) {
      struct ipList **list;
      int i;
      
      list = malloc( MAXLIST * sizeof( struct ipList * ) );
      i = 0;
      while ( i < MAXLIST ) {
        list[ i ] = malloc( sizeof( struct ipList ) );
        list[ i ]->id = i;
        list[ i ]->name = "citibank.co.cx";
        list[ i ]->ip = "209.249.147.15";
        i++;
      }
      
      return ( list );
    }
    
    void printList( struct ipList **list ) {
      int i = 0;
      
      while ( i < MAXLIST ) {
        printf( "%s - %s\n", ( list[ i ]->name ), ( list[ i ]->ip ) );
        i++;
      }
    }
    *Cela*

  6. #6
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Though im still not sure how:
    malloc( MAXLIST * sizeof( struct ipList * ) );
    differs from:
    malloc( MAXLIST * sizeof( struct ipList ) );
    exactly. I do understand what I was doing wrong with the rest. Thanks, it works now.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by dharh
    Though im still not sure how:
    malloc( MAXLIST * sizeof( struct ipList * ) );
    differs from:
    malloc( MAXLIST * sizeof( struct ipList ) );
    exactly.
    The first requests memory for MAXLIST pointers to objects of type struct ipList; the second requests memory for MAXLIST objects of type struct ipList. Maybe the following will help clarify.
    Code:
    #include <stdio.h>
    
    #define MAXLIST 10
    
    struct ipList
    {
        int id;
        char *name;
        char *ip;
    };
    
    int main(void)
    {
        printf("MAXLIST = %d\n", MAXLIST);
        printf("sizeof(struct ipList) = %lu\n", (unsigned long)sizeof(struct ipList));
        printf("sizeof(struct ipList *) = %lu\n", (unsigned long)sizeof(struct ipList *));
        printf("MAXLIST * sizeof(struct ipList) = %lu\n", (unsigned long)(MAXLIST * sizeof(struct ipList)));
        printf("MAXLIST * sizeof(struct ipList *) = %lu\n", (unsigned long)(MAXLIST * sizeof(struct ipList *)));
        return 0;
    }
    
    /* my output
    MAXLIST = 10
    sizeof(struct ipList) = 12
    sizeof(struct ipList *) = 4
    MAXLIST * sizeof(struct ipList) = 120
    MAXLIST * sizeof(struct ipList *) = 40
    */

  8. #8
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    That explains some of the problems ive had in the past.

    I guess the final question I have is:

    Code:
    struct ipList **getList( void ) {
      static struct ipList *list[ MAXLIST ];
      int i;
      
      //list = malloc( MAXLIST * sizeof( struct ipList * ) );
      i = 0;
      while ( i < MAXLIST ) {
      	list[ i ] = malloc( sizeof( struct ipList ) );
        list[ i ]->id = i;
        list[ i ]->name = "citibank.co.cx";
        list[ i ]->ip = "209.249.147.15";
        i++;
      }
    
      return ( list );
    }
    Is there a way to not use static in this case:
    struct ipList *list[ MAXLIST ];

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dharh
    That explains some of the problems ive had in the past.

    I guess the final question I have is:

    Is there a way to not use static in this case:
    struct ipList *list[ MAXLIST ];
    Sure, use a global variable.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    No way inside the function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. returning pointers to structures
    By Giant in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2005, 08:40 AM
  5. 2 Dimenstional Array vs Array of Structures
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2004, 04:51 AM