Dear Banfa
Thanks for your reply. Your suggestion works well.
I also found an alternate way to do it. In fact somewhere in the web, I read that when declaring a struct, one has to know the array size apriori. But this is not true. For instance, the following program works
- //---------------------------------------------------------------------------------------
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
struct sv {
-
float **a;
-
};
-
-
main () {
-
int i;
-
struct sv *p;
-
p = (struct sv *) malloc (sizeof(struct sv)*2);
-
p->a = (float **) malloc (sizeof(float)*10);
-
(p+1)->a = (float **) malloc (sizeof(float)*10);
-
for(i=0;i<10;i++)
-
{
-
*(p->a+i) = (float *) malloc (sizeof(float)*10);
-
*((p+1)->a+i) = (float *) malloc (sizeof(float)*10);
-
*(*((p+0)->a+i)+2) = i;
-
p->a[i][i]=i; //Suggested by Banfa
-
printf("%f %f %f\n",*(*((p+1)->a+i)+2),*(*(p->a+i)+2),p->a[i][i]);
-
}
-
}
-
//-----------------------------------------------------------------------------------------
-
The output will be
0.000000 0.000000 0.000000
0.000000 1.000000 1.000000
0.000000 2.000000 2.000000
0.000000 3.000000 3.000000
0.000000 4.000000 4.000000
0.000000 5.000000 5.000000
0.000000 6.000000 6.000000
0.000000 7.000000 7.000000
0.000000 8.000000 8.000000
0.000000 9.000000 9.000000
I spent one full day in this. I felt that I should share this experience with others so that they need not spent time at all. When I was stuck I found that there is no single book which talks about multi-dim array inside a structure. Also no online notes or manuals discuss about this. If anybody knows a good book or website which discusses multi-dim arrays inside a struct, kindly let me know.
Thanks in advance.
Baskar