Thread: Using strcpy on a struct

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    Using strcpy on a struct

    Hi, I am trying to use strcpy on a member from a structure, but my program crashes when i try to run it.

    Here are the structures:

    Code:
    typedef struct
    {
    	char last[NAMESIZE];
    	char first[NAMESIZE];
    }name;
    
    typedef struct
    {
    	int id;
    	name name;
    	float score;
    }record;
    
    typedef struct
    {
    	record *data;
    	size_t nalloc;
    	size_t nused;
    }record_list;
    and here is the code which is in a function that crashes the program:

    Code:
    int list_insert(record_list *list, const record *rec)
    {
    	size_t i;
    	list->nalloc = 0;
    	list->nused = 0;
    	
    	strcpy(list->data[list->nused].name.first, "michael");
    	
    	return 0;
    }

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Are you allocating enough memory to the list->data[] array? The code I can see only declares list->data to be a pointer.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Oh right forgot about that, thanks for reminding me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  2. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  3. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  4. strcpy
    By Tibo in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 07:02 AM
  5. strcpy()
    By Megatron in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:42 AM