array of structure problems

This is a discussion on array of structure problems within the C++ Programming forums, part of the General Programming Boards category; Hi all, Im trying to make an array of structures Code: struct infoAttr{ char Name[20]; int age }; infoAttr p1 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 11:34 AM
  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, 12:36 PM

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