R
romesh
Hi all,
I want to create a dynamic array of a structure and fill the
structure in a function. Then I read the structure and that is where I
am getting problems.
struct s1 {
char m1[5];
char m2[5];
};
struct s2 {
char m3[5];
int m4;
};
struct s3 {
struct s1 m5;
struct s2 **m6; /* Iwant a dynamic array of s2 type; */
}
int f1 (void *par){
struct s1 a1;
int no, i;
char a[5];
no =10;
(*(struct s3 *)par).m6 = malloc(sizeof(struct s2 *) *
no);
for (i=0; i<no; ++i)
{
strcpy(a, "");
sprintf(a, "Hi%d", i);
(*(struct s3 *)par).m6 = malloc(sizeof(struct s2 ) );
memcpy(&(*(struct s3 *)par).m6.m3, a, 4); /* trying to
cope Hi+i into each m3 of m6 */
}
return no;
}
int main(){
struct s3 val;
int no, i;
no = f1(&val);
for(i=0; i<no; ++i)
{
printf("%c%c", val.m6.m3[0], val.m6.m3[0]);
/* I do not know how to access m6 and m3 */
}
return 0;
}
Can you please help me.
Thanks in advance.
Romesh.
I want to create a dynamic array of a structure and fill the
structure in a function. Then I read the structure and that is where I
am getting problems.
struct s1 {
char m1[5];
char m2[5];
};
struct s2 {
char m3[5];
int m4;
};
struct s3 {
struct s1 m5;
struct s2 **m6; /* Iwant a dynamic array of s2 type; */
}
int f1 (void *par){
struct s1 a1;
int no, i;
char a[5];
no =10;
(*(struct s3 *)par).m6 = malloc(sizeof(struct s2 *) *
no);
for (i=0; i<no; ++i)
{
strcpy(a, "");
sprintf(a, "Hi%d", i);
(*(struct s3 *)par).m6 = malloc(sizeof(struct s2 ) );
memcpy(&(*(struct s3 *)par).m6.m3, a, 4); /* trying to
cope Hi+i into each m3 of m6 */
}
return no;
}
int main(){
struct s3 val;
int no, i;
no = f1(&val);
for(i=0; i<no; ++i)
{
printf("%c%c", val.m6.m3[0], val.m6.m3[0]);
/* I do not know how to access m6 and m3 */
}
return 0;
}
Can you please help me.
Thanks in advance.
Romesh.