Thread: array of structure problems

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    array of structure problems

    Hi all,

    Im trying to make an array of structures
    Code:
    struct infoAttr{
    	char Name[20];
    	int age
    };
    
    
    infoAttr p1 = {"mike", 20};
    infoAttr p2 = {"Dave",23};
    infoAttr p3 = {"Alex", 23};
    
    infoAttr people[3] = {p1,p2,p3};
    Im getting an error with the last line saying:

    "error C2440: 'initializing' : cannot convert from 'struct infoAttr' to 'char'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called"

    Its probably really simple but its got me stumped.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why not just do
    Code:
    infoAttr people[3] = {
      {"mike", 20},
      {"Dave",23},
      {"Alex", 23},
    }
    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.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    What Salem said, although Mike is 22, and I would advise using std::string instead of char arrays. Just much easier, IMO. You may want to overload an = operator or something if you want to directly assign one struct to another.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks for the replies. I've been using your example now for a few days, but unfortunately I now need to expand on it.

    Lets say my structure is used to store information of every house hold in a road so its like this:

    Code:
    struct infoAttr{
    	char Name[20];
    	int houseNumber;
    };
    
    infoAttr road1[3] = {
      {"mike", 20},
      {"Dave",23},
      {"Alex", 22}
    };
    
    infoAttr road2[3] = {
      {"Rich", 27},
      {"Steve",13},
      {"Andy", 29}
    };
    I want to combine the above array's into a 2d array called all roads like so

    Code:
    infoAttr allRoads[2][3] = {road1,road2};
    However im getting the error message

    Code:
    error C2440: 'initializing' : cannot convert from 'struct infoAttr [3]' to 'char'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Any ideas? Thanks :-)
    Last edited by cloudy; 09-05-2006 at 01:15 AM.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Sorry I didnt make myself clear. The problem im having isnt with the line allRoads[0][1], thats just showing what im attempting to do. The problem im having is with the line above it, the infoAttr allRoads[2][3] = {road1,road2};

    I'll remove the allRoads[0][1] bit from the above code as that makes it confusing :-)

    So the following line gives the error:
    Code:
    infoAttr allRoads[2][3] = {road1,road2};
    gives error:

    Code:
    error C2440: 'initializing' : cannot convert from 'struct infoAttr [3]' to 'char'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > infoAttr allRoads[2][3] = {road1,road2};
    You can't assign arrays.

    You could point at them though, with
    infoAttr *allRoads[2] = {road1,road2};
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. Problem(s) with an array!!
    By Leojeen in forum C Programming
    Replies: 6
    Last Post: 05-02-2008, 07:26 PM
  3. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  4. array in structure help
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 07:51 AM
  5. Array sorting problems
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 01:36 PM