Like Tree1Likes
  • 1 Post By TheBigH

Using strcpy on a struct

This is a discussion on Using strcpy on a struct within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to use strcpy on a member from a structure, but my program crashes when i try ...

  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.
    ogglock likes this.
    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, 11:34 AM
  4. strcpy
    By Tibo in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 06:02 AM
  5. strcpy()
    By Megatron in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 06:42 AM

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