Thread: multidimensional character array question

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    37

    multidimensional character array question

    Dear all:

    I have a structure that goes that this:

    Code:
    typedef struct someStruct {
            char name[30];
            int number;
    } someStruct_t;
    Then in main(), I declare an array of structures:

    Code:
    someStruct_t structures[3];
    and I declare an array of names:

    Code:
    char names[3][30] = { "Mary", "John", "Lily" };
    When I tried to assign the structure member "name" to the first string of "names" like the following:

    Code:
    structures[0].name = names[0];
    It tells me that "incompatible types when assigning to type ‘char[30]’ from type ‘char *’"

    But I thought they are both addresses, the value of structures[0].name should be able to be assigned the value of names[0], what did I do wrong?

    Any help is appreciated, thanks

    barramundi9

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    16
    Arrays can't be assigned.
    You have to use strcpy (in C) or std::string (C++).

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    37
    Got it working, thanks so much, TomasRiker.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Could I ask if you already have an array with all the names in it, why did you not choose to declare "char name[30]" as a pointer and have it point to the names in the array?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character array question
    By blindchicken11 in forum C Programming
    Replies: 5
    Last Post: 11-27-2011, 06:14 PM
  2. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  3. Question about string & character array
    By Roy01 in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2006, 09:50 AM
  4. multidimensional array function - simple question
    By boltz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2005, 04:24 PM
  5. multidimensional array question
    By Mr_Jack in forum C++ Programming
    Replies: 10
    Last Post: 11-01-2003, 05:54 AM