Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts

    C Structure Arrays

    I'm trying to use an array of structures in C to store information about people. Each person's information would be stored in an index of the array (a structure).

    I can build and read an array of a preset number of structures like this:
    Code:
    #include <stdio.h>
    
    struct person{
        int id;
        char *name;
    };
    
    int main(){
        struct person testst[2];
        struct person *ttestst0;
        struct person *ttestst1;
        
        testst[0].id=1;
        testst[0].name="person 1";
        
        testst[1].id=2;
        testst[1].name="person 2";
        
        ttestst0=&testst[0];
        ttestst1=&testst[1];
        
        printf("%d\n",ttestst0->id);
        printf("%s\n",ttestst0->name);
        printf("%d\n",ttestst1->id);
        printf("%s\n",ttestst1->name);
    }
    But this only lets me define a preset number of structures (in this case, 2). I tried using struct person *testst; and it works, but I get a "Segmentation fault" error at the end of execution. Also, it seems I have to use "ttestst0" and "ttestst1" to read information from the "testst" structures.

    I need to be able to define any number of instances of the "person" structure on the fly. And eventually I'm going to have to move around structures in the array (because I'll be removing structures from the middle of the array). How would I do this?

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    For a dynamic array size, you'll be needing to use dynamic memory allocation. For straight C, check out malloc() and friends. If C++, new and delete are the way to go - and a little easier than doing it manually in C. Google should provide a bevy of resources on either option.

  • #3
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I'm using C, not C++.

    I've modified my main function a bit so I don't need to define extra structures to read the original. Then I tried to allocate more memory for a third structure in the array. This is my main function now:
    Code:
    int main(){
        struct person testst[2];
        
        testst[0].id=1;
        testst[0].name="person 1";
        
        testst[1].id=2;
        testst[1].name="person 2";
        
        printf("&#37;d\n",testst[0].id);
        printf("%s\n",testst[0].name);
        printf("%d\n",testst[1].id);
        printf("%s\n",testst[1].name);
        
        // now try to allocate more memory for it
        struct person* testst[2]=(struct person*) malloc(sizeof(struct person));
        
        testst[2].id=3;
        testst[2].name="person 3";
        
        printf("%d\n",testst[2].id);
        printf("%s\n",testst[2].name);
    }
    Maybe I'm misunderstanding the use of this function. I get several errors when I compile this, including conflicting types and invalid initializer. I'm still researching this, but I'm not having much luck.

    EDIT: I tried struct person* testst=(struct person*) malloc(3*sizeof(struct person)); and got fewer errors. I still get the conflicting types error and an imcompatible implicit declaration warning, though.
    Last edited by thesmart1; 06-02-2008 at 04:07 AM.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
        struct person testst[2];
    .......
        struct person* testst[2]=(struct person*) malloc(sizeof(struct person));
    you can't mix static and dinamic allocation for the same variable.

    you need to allocate memory for the array and then for each element of the array:
    Code:
       struct person * atestst = (struct person *)malloc(10*sizeof(struct person *));
    this allocate memory for 10 struct person, pointers initialised with 0/null, but don't create each item.

    Code:
       for(int i=0;i<10;i++){
          atestst[i] = (struct person *)malloc(sizeof(struct person));
       }
    this allocate memory for each item.

    PS: it's a long time I never write something in c, test it and let's me know if I'm wrong somewhere,

    regards

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    The code oesxyl posted:

    Code:
    struct person * atestst = (struct person *)malloc(10*sizeof(struct person *));
    Will create an array of 10 structs, you don't need to allocate each one like the second bit of code he posted since this is a one dimensional array. You only need to do that if you use 2 or more dimensions. And with that code you can access it just like a non-dynamic array (e.g. atestst[0] to atestst[9]).
    OracleGuy

  • #6
    Regular Coder thesmart1's Avatar
    Join Date
    Dec 2005
    Posts
    369
    Thanks
    7
    Thanked 3 Times in 3 Posts
    OK, I have this main function now:
    Code:
    int main(){
        struct person * testst = (struct person *)malloc(3*sizeof(struct person *));
        
        testst[0] = (struct person *)malloc(sizeof(struct person));
        testst[1] = (struct person *)malloc(sizeof(struct person));
        
        testst[0].id=1;
        testst[0].name="person 1";
        
        testst[1].id=2;
        testst[1].name="person 2";
        
        printf("%d\n",testst[0].id);
        printf("%s\n",testst[0].name);
        printf("%d\n",testst[1].id);
        printf("%s\n",testst[1].name);
        
        testst[2] = (struct person *)malloc(sizeof(struct person));
        
        testst[2].id=3;
        testst[2].name="person 3";
        
        printf("%d\n",testst[2].id);
        printf("%s\n",testst[2].name);
    }
    I'm getting "incompatible types in assignment" errors where i define each of the three structs (the highlighted lines).

    Also, I assume I can start out by allocating memory for one struct and then go from there, like with struct person * testst = (struct person *)malloc(sizeof(struct person *)); in the first line, correct?

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    see oracleguy post and try how it said.

    also try to change the array allocation to:
    Code:
    struct person * testst = (struct person *)malloc(3*sizeof(struct person));
    I remove the last *.

    as I said, it's a long time,

    regards

  • #8
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by thesmart1 View Post
    I'm getting "incompatible types in assignment" errors where i define each of the three structs (the highlighted lines).
    That is because testst[0] or testst[1] is dereferencing the testst pointer so you can't/shouldn't assign it a type of struct person * because it is type struct person

    Also you aren't deallocating your memory at any point in your program. Which means as it stands now, that code is leaking memory. Which means that it will never get marked as available even after the program exits. You need to free any dynamic memory using the free function.

    And to your other question, yes you could start off with one and then increase it later, like so:

    Code:
    struct person * testst = (struct person *)malloc(1*sizeof(struct person *));
    
    // use it.
    
    struct person * temp = (struct person *)malloc(3*sizeof(struct person *));
    temp[0] = testst[0]; //Copy data from old array into the new one
    free(testst);
    testst = temp;
    
    // now you have a 3 element array.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •