...

View Full Version : C Structure Arrays



thesmart1
06-02-2008, 12:49 AM
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:

#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?

tomws
06-02-2008, 01:57 AM
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.

thesmart1
06-02-2008, 03:03 AM
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:

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.

oesxyl
06-02-2008, 03:40 AM
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:


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.



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

oracleguy
06-02-2008, 04:01 AM
The code oesxyl posted:


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]).

thesmart1
06-02-2008, 04:06 AM
OK, I have this main function now:

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?

oesxyl
06-02-2008, 04:13 AM
see oracleguy post and try how it said.

also try to change the array allocation to:


struct person * testst = (struct person *)malloc(3*sizeof(struct person));

I remove the last *.

as I said, it's a long time, :)

regards

oracleguy
06-02-2008, 05:59 AM
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:



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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum