Thread: Accessing Array of strings in C Structure

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    27

    Accessing Array of strings in C Structure

    Hi,

    I'm working on my second C program, but I can't figure out
    how to access the Array of strings in the C Structure below.

    Could someone please show me how ? I'm really lost on this.
    I need to use pointers to access the structure.

    * My code
    Code:
    #include "stdafx.h"
    #include <string.h>
    
    typedef struct st {
    		char *name;
    		int	id;
    		char *items[3];    // MAKE IT AN ARRAY
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	struct st my_emp;
    	struct st *pmy_emp;
    
                   //ADDED
                  pmy_emp = (struct st *) malloc(sizeof(struct st));
    
    	my_emp.name="Someones Name";
    	my_emp.id = 6;
    	pmy_emp = &my_emp;
    
                    // CHANGED
                    strcat(my_emp.items[0],"Cats");
    	strcat(my_emp.items[1],"Dogs");
    
    	//WAS
                    //pmy_emp->items = "Cats","Dogs";
    
    	printf ("STRUCT TEST...  = %s\n", pmy_emp->name);
    	
    	for(int x=0;x < 2; x++){
    			printf ("ITEMS = %s\n", pmy_emp->items[x]);
    	}
    
    	return 0;
    	}
    END ************

    Thanks,
    -Areal
    Last edited by Areal Person; 05-29-2010 at 06:42 PM.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    First of all, you don't have any arrays of strings, you have arrays of chars.

    you haven't allocated any memory for the arrays. You need to malloc them some memory before you can use them if you declare then like that (I think). And I don't think you can assign strings like that either, not if they have been malloc'd. Especially the second one - C isn't going to like that comma one bit ^_^. Look into the function:

    malloc - C++ Reference
    strcat - C++ Reference
    Last edited by KBriggs; 05-29-2010 at 06:29 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    27
    Ok,

    I added a malloc, and used strcat -- but it still bombs ?

    I also made char * items an array - with no luck.

    Any ideas ??

    Thanks much,
    -Areal
    Last edited by Areal Person; 05-29-2010 at 06:43 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct foo
    {
        char *arrayofstrings[ 3 ];
    };
    ...
    
    struct foo bar;
    
    bar.arrayofstrings[ 0 ] = malloc( somenumberofcharacters );
    
    ...
    
    strcpy ( bar.arrayofstrings[ 0 ], "hello world" );
    
    ...
    
    printf( "%s\n", bar.arrayofstrings[ 0 ] );
    
    free( bar.arrayofstrings[ 0 ] );

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

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    27
    Thanks That did It.

    QUESTION:

    I'm guessing I need to call malloc for each item[element] // Correct ?

    I would just put it in a for loop and use a varaible for each element ?

    Right ?

    Or is there a better way, so I just use one call to malloc ?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yep. A loop is fine. Be sure to free each one as well.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array and structure problem
    By bluetxxth in forum C Programming
    Replies: 3
    Last Post: 04-13-2010, 06:56 AM
  2. pre-defined array of strings within a structure
    By jamie85 in forum C Programming
    Replies: 1
    Last Post: 02-06-2006, 04:56 PM
  3. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 06:05 PM