Like Tree1Likes

multiple attributes in arrays

This is a discussion on multiple attributes in arrays within the C++ Programming forums, part of the General Programming Boards category; heya, basic question: Array given with say 10 entries: array[0] = id1; array[1] = id2; etc... I want to store ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    38

    multiple attributes in arrays

    heya,

    basic question:

    Array given with say 10 entries:
    array[0] = id1;
    array[1] = id2;
    etc...


    I want to store multiple attributes for every array entry, like this:
    array[0].date = 123;
    array[0].age = 234;
    array[0].year = 345;
    etc...

    how can this be done?? :/

    or better: how must the array be created in order for this to work?

    any direction or example would be more than welcome!!

    btw. the attributes are all the same for every arrayindex.

    in the next step, I will have to store arrays in there as I need to store multiple attributes for every arrayindex, say array[0].date[0], array[0].date[1], etc etc

    thanks in advance!!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    An array can only hold one type of variable, perhaps you should look into using a structure.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    I use the array as hash table to store an id though :/
    I however need to store multiple datasets for every id

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    basically I have an item name and id.

    I have a hashtable in which I store the id. I access the id with a hasfunction on the name which returns the index.

    now for every single item I have to store multiple datasets (referring to different days), like this:
    hashtable[0].amount[0]=123;
    hashtable[0].amount[1]=234;
    hashtable[0].amount[2]=345;
    hashtable[0].added[0]=321;
    hashtable[0].added[1]=432
    bla...
    meaning the amount of the item in pos 0 on day 0 was 123, on day 1 was 234, etc etc.

    I dunno how this can be done with arrays/structs =/

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    This is what structures were defined for, holding multiple related items. So instead of returning an index to an int return an index to an array of your structure.

    Jim

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    oh I see... would this work?
    Code:
    typedef struct{
    int amount[10];
    int added[10];
    } item;
    
    [.....]
    
    item *id;
    id = malloc(sizeof(item)*10);
    id[0].amount[0]=123;
    id[0].amount[1]=234;
    id[0].amount[2]=345;
    ...
    thx for the help ^^

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    Possibly. What exactly are you trying to accomplish. There may be better ways than using the arrays in the structure. And don't forget this structure can hold things other than just int arrays. For example if this was an inventory system it could hold the item name and item Id number along with the number of parts on hand and number of parts used.

    Jim

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Why are you using malloc instead of new?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Quote Originally Posted by Elysia View Post
    Why are you using malloc instead of new?
    because I just started with c++ ^^ can I declare an array of structs with new too?

    Quote Originally Posted by jimblumberg View Post
    Possibly. What exactly are you trying to accomplish. There may be better ways than using the arrays in the structure. And don't forget this structure can hold things other than just int arrays. For example if this was an inventory system it could hold the item name and item Id number along with the number of parts on hand and number of parts used.

    Jim
    I mentioned in post above what I try to accomplish ^^
    quote:
    basically I have an item name and id.

    I have a hashtable in which I store the id. I access the id with a hasfunction on the name which returns the index.

    now for every single item I have to store multiple datasets (referring to different days), like this:
    hashtable[0].amount[0]=123;
    hashtable[0].amount[1]=234;
    hashtable[0].amount[2]=345;
    hashtable[0].added[0]=321;
    hashtable[0].added[1]=432
    bla...
    meaning the amount of the item in pos 0 on day 0 was 123, on day 1 was 234, etc etc.

    I dunno how this can be done with arrays/structs =/

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    I mentioned in post above what I try to accomplish
    That doesn't really explain what you are trying to accomplish. What do those numbers represent? Where are these numbers coming from? Why are you putting them in a hash table? Etc, etc.

    Jim

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Quote Originally Posted by coffee_cup View Post
    because I just started with c++ ^^ can I declare an array of structs with new too?
    Yes, new can do everything malloc can and more. But you shouldn't use it. You should use appropriate containers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by coffee_cup View Post
    basically I have an item name and id.

    I have a hashtable in which I store the id. I access the id with a hasfunction on the name which returns the index.

    now for every single item I have to store multiple datasets (referring to different days), like this:
    hashtable[0].amount[0]=123;
    hashtable[0].amount[1]=234;
    hashtable[0].amount[2]=345;
    hashtable[0].added[0]=321;
    hashtable[0].added[1]=432
    bla...
    meaning the amount of the item in pos 0 on day 0 was 123, on day 1 was 234, etc etc.

    I dunno how this can be done with arrays/structs =/
    I would not put any arrays inside the struct. If you need to have two indices, I would do a two dimensional array, where each element is one struct.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    thx for the help all

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    hm I have a new issue... with initializing structs in a class :/

    instead of using arrays inside structs, I went for a structarray now.

    all I want to do is initialize the struct so I can use it in the set function of my class:

    Code:
    class hashclass
    {
    private:
        int mysize;
    
    
        struct hashstruct
        {
            int value;
            string teststring;
            struct dataset *ptr;
        };
    
    public:
        hashclass()
        {
            unsigned int mysize = 500;
            struct hashstruct myhash[mysize];
        }
    
        ~hashclass()
        {
        }
    
        void set(string name, int value)
        {
            myhash[value].teststring= name;
        
        }
     
    }
    myhash is not declared as its only the constructor of hashclass

    so I try to init it in private first:


    Code:
    class hashclass
    {
    private:
        int mysize;
        struct hashstruct *myhash;
    
        struct hashstruct
        {
            int value;
            string teststring;
            struct dataset *ptr;
        };
    
    public:
        hashclass()
        {
            unsigned int mysize = 500;
            myhash = new hashstruct[mysize];
        }
    
        ~hashclass()
        {
        }
    
        void set(string name, int value)
        {
            myhash[value].teststring = name;
            ...
        }
        ...
    }
    but this won't work =/ any chance you could point me to the issue? :/

    thx in advance

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    The compiler sees your definition "struct hashtruct *myhash" as a declaration of a struct type outside the hashclass and a definition of a pointer to such a struct.

    Simple fix to that is to move the "struct hashstruct *myhash" so it appears after the definition of the struct type.

    That will get your code to compile, but not to run.

    Then there are future runtime errors, since the "mysize" inside the hashclass contructor shadows the member named mysize. The member named mysize is never initialised, which will cause no end of problems for any code which relies on it actually storing the number of elements in myhash.

    In the constructor of hashclass, it is better to use an initialiser list.
    Code:
     hashclass() : mysize(500), myhash(new hashstruct[500])
        {
           
        }
    Since your constructor allocates memory, your destructor needs to release it.

    And you need to implement copy constructor and assignment operator so they will work properly with the default constructor and destructor.


    Your code is also obviously relying on a "using namespace std;" or (maybe) a "using std::string;" being in effect. That is a bad idea for all sorts of reasons which you're nowhere near ready to learn about yet. The rough rule is to not use such statements in a header file.
    Last edited by grumpy; 03-28-2013 at 05:17 PM.
    coffee_cup likes this.
    Right 98% of the time, and don't care about the other 3%.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting up a Class to hold Objects with multiple attributes
    By IPthereforIam in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2012, 10:50 AM
  2. Searching Multiple Arrays?
    By Cell in forum C Programming
    Replies: 3
    Last Post: 03-19-2009, 03:45 PM
  3. classs with multiple arrays
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 05-06-2007, 08:31 PM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 10:14 PM

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