1

Possible Duplicate:
How can I create a dynamically sized array of structs?

I'm new to C and learning online really helps but now i'm really stuck. I want to have an array of structures ACout, with size ACcount

typedef struct{
int outcount;
int *lingoout;
double **likegoout;
} ACout;

// int outcount is the size of the array lingoout and likegoout is a 2d array with number of rows being outcount and number of columns 4

//int outcount will be calculated and will be different for each ACout.

int ACcount=0; //global, outside main

in main (){
 //HOW TO INITIALIZE THE ARRAY OF STRUCTS???
 //LIKE THIS?
  ACout* ACout_array = (ACout *) calloc (1, sizeof (ACout));
  if ( ACout_array == NULL) {
printf("Cannot allocate initial memory for data\n");
exit(1);
   }
 }

in some function {
ACcount++;
//int lingoout is computed here, as n; 
//also array lingout, as lin[n] 
//2d array likegoout, as likeout[n][4] is ready here

//how can I write a function to add one more structs to the array?
addarray(ACcount, AC_array);

//how can I put my calculated values to the structs?

 AC_array[ACcount]->outcount=n;
AC_array[ACcount]->lingoout=lin;
AC_array[ACcount]->likegoout=likeout;

}

void addACarray (ACout **ACout_array, int ACcount) {

  ACout *temp = (ACout*)realloc(*ACout_array, (ACcount * sizeof(ACout)));

  if(temp == NULL) {
    printf("Cannot allocate growarray memory!\n");
  }
  else
    *ACout_array = temp;
}

Thanks for your help! Much appreciated!!! ---Helen

4
  • shouldnt that be something like typedef struct Something { int outcount; int *lingoout; double **likegoout; } ACout;? Commented Oct 31, 2012 at 2:58
  • @abhinole yes that was a typo.. thanks! Commented Oct 31, 2012 at 3:02
  • @Xymostech it's not quite the same. do i have to use the list? and in my struct, i have a 2d array. Commented Oct 31, 2012 at 3:06
  • @aqualeeva Perhaps look at this answer: stackoverflow.com/a/261019/57318 Commented Oct 31, 2012 at 3:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.