Thread: How to store a char array into another char array?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    42

    How to store a char array into another char array?

    OK so I have this struct:

    Code:
    struct person{
           char first[30];
           char last[30];
           char month[11];
           int day;
           int year;
           int num_month;
           int total_days;
           };
    I need to transfer all of the data into a temp struct. I originally tried using:
    Code:
    temp[mc] = people[count1];
    This worked on my home computer and the people were correctly sorted but when I tried it on one at school it crashed.

    My next thought was to break it down and store each member individually like:

    Code:
      temp[mc].first = people[count1].first;
                temp[mc].last  = people[count1].last;
                temp[mc].month  = people[count1].month;
                temp[mc].day = people[count1].day;
                temp[mc].year = people[count1].year;
                temp[mc].num_month = people[count1].num_month;
                temp[mc].total_days = people[count1].total_days;
    And this works for the integers, but I'm getting incompatible types for the three char array members (first, last, month).

    I don't really know the correct way to do this so can anyone help me out? Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by DaNxTh3xMaNx View Post
    I need to transfer all of the data into a temp struct. I originally tried using:
    Code:
    temp[mc] = people[count1];
    This worked on my home computer and the people were correctly sorted but when I tried it on one at school it crashed.
    Assignment of structs should definitely work, and should definitely be preferred over breaking down the struct and reconstructing it into a new one, as there's no way you can miss out members of it if you add new ones.

    Try a really simple program that does what you want (i.e. initialize, then copy, then make sure the copied struct is the same as the original one) and make sure it runs on your school computer. If that succeeds, the problem may well be to do with your array indexing (I'd guess...) or pointer manipulation that somehow your home and school compilers treat differently.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > This worked on my home computer and the people were correctly sorted
    As it should.
    C supports assigning one struct to another in the manner you describe.

    > but when I tried it on one at school it crashed.
    Crashed how?
    If it's some brain-damaged ancient fossil compiler, then it might not even support struct assignments.

    The ability to assign a struct was introduced formally in ANSI-C (the original, over 20 years ago).
    If you're still being taught using that, perhaps you might wonder what exactly you're learning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    no the computers are decent (im at a university)

    it just pops up and says birthday.exe stopped working.

    i ran through the debugger and it just stops once i go over my mergesort call, but if i go into it it goes through but still stops once I get to the merge part of the mergesort. it doesn't say access violation and (when in the debugger) it doesn't pop up with the stopped working message. it just literally does nothing.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    *working*
    Last edited by DaNxTh3xMaNx; 11-12-2010 at 10:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM