Here is my problem. First i define a struct with type organism. it includes 1 char and one int pointer (later used as an array).
Then i initialize the struct to a pointer with name Agent.
After that i want to allocate a size of space only to the pointer(array) dna, but it doesn't work and i don't know why. i read my book about structs, pointers and arrays, but didn't give me a solution.

here is the code:

Code:
struct organism
{
   char fitness;
   int *dna;
};
void main()
{
   struct organism *Agent; //intiialize struct organism to pointer Agent
   Agent.dna = malloc(5*sizeof(int)); //TRY to allocate 5times the dna integer to get an array
   Agent.dna[3] = 12;  //try to set a value into index 3 of the dna-array
}



then it fails and the error message is: subscript requires array or pointer type.
what it do, is to allocate 5 times memory for the whole agent and not only for the dna(array).

Last edited by PriNova; 09/12/12 18:12.