Thread: strcpy - problem!!!

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Unhappy strcpy - problem!!!

    Hi,

    I tried looking for threads for strcpy specifically for my problem but i just can't find the specific answers. Anyway, here's my problem....

    I am trying to copy an array of string to my structure but i'm always getting an Unhandled Exception and points me to strcat.asm. Actually it is weird because in my program, it was not having the error not until i changed the value i passed. I tried several approach to test and debug why it is happening but it just keeps on giving the error.

    Below is a snippet of my code for your reference. I'm trying to copy a string to two differently declared structures. I hope someone could help me... ASAP!

    Code:
    typedef struct st_personal_info
    {
    	char	sName[51];
    	int	iAge;
    	char sAddress[501];
    	char	sBirthday[11];
    }ST_PERSONAL_INFO;
    
    typdef struct st_field_values
    {
    	char sField[6];
    	char sValue[51];
    } ST_FIELD_VALUES;
    
    ...
    
    int main()
    {
    	ST_PERSONAL_INFO *lst_personalInfo;
    	ST_FIELD_VALUES lst_fieldValues;
    	char lsToPass[11] = "MICHAEL";
    
    	strcpy(lst_fieldValues.sValue, lsToPass);
    	strcpy(lst_personalInfo->sName, lst_fieldValues.sValue);
    ...
    
    }
    I am having the exception at "strcpy(lst_personalInfo->sName, lst_fieldValues.sValue);" and I don't know why. Please help.

    Many thanks everyone!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to allocate memory for the record pointer.

    ST_PERSONAL_INFO *lst_personalInfo = malloc(sizeof(ST_PERSONAL_INFO));

    Don't forget to free it when you're done.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    31
    Im familiar with the "new" call for C++ so seeing the C version is new for me.

    Is there any difference between malloc and new?

    edit: oops! didnt notice the C board
    Last edited by JohnLeeroy; 01-15-2011 at 08:55 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    JohnLeeroy, new() is strictly a C++ function. This is the C forum, it's safe to assume that using C's malloc is required.

    Oh, and cross-posted.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JohnLeeroy View Post
    Im familiar with the "new" call for C++ so seeing the C version is new for me.

    Is there any difference between malloc and new?

    edit: oops! didnt notice the C board
    Well, at some point new probably calls malloc() but it also includes logic to activate constructors in classes.

    Malloc just gives you a blob of memory to work with.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Talking

    Quote Originally Posted by CommonTater View Post
    You need to allocate memory for the record pointer.

    ST_PERSONAL_INFO *lst_personalInfo = malloc(sizeof(ST_PERSONAL_INFO));

    Don't forget to free it when you're done.
    I tried your solution. Well... it worked! thanks a lot! really appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's up with this strcpy?
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 05:24 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM

Tags for this Thread