qi*****@gmail.com wrote:
Hi I have a very complicated struct
typedef struct F {
int a_len;
struct A *a[32] /* OPTIONAL */;
B b;
C *c /* OPTIONAL */;
D *d /* OPTIONAL */;
E *e /* OPTIONAL */;
} F_t
What do you mean by "OPTIONAL"? There aren't any optional fields
in a structure.
A,B,C,D,E are all struct for instance
typedef struct A{
int len;
unsigned char addr[9];
}A_t
And I have another structure on top of F
typedef struct G{
int len;
F_t F[32];
}G_t
typedef struct H{
...
G_t *g;
}H_t
I have thinking of writing a function to evaluate struct F, and when I
eveluate struct H, I will call this function.
I don't see how these additional structure typedefs are relevant
to the question you ask below. Am I missing something important?
I use malloc in the function to evaluate F, will it cause any problem?
will the point link lost when I exit this function?
Am I right in assuming that by 'evaluate' you mean initialize?
void evluate_F(F_t *f, ...)
If the three dots are meant to indicate that the function takes a
vraiable number of arguments things could become interesting since
I don't see an easy way to determine the number and types of the
arguments from 'f'. But if they just mean that you don't want to
show the remaining arguments it should be fine.
{
char *tmp;
int i;
A_t tmpA;
f->a_len = 10;
for(i=0;i<;f->a_len;i++)
Here you have a stray ';'. The compiler will be upset;-)
{
strcpy(tmpA.addr,"1234");
tmpA.len = strlen(tmpA.addr);
if((tmp=malloc(sizeof(A_t)))!=NULL)
{
memcpy(tmp,tmpA,sizeof(tmpA));
Here you need
memcpy( tmp, &tmpA, sizeof(tmpA) );
since 'tmpA' is a structure, not a pointer to a structure. More-
over, you could simplify this to
*tmp = tmpA;
since you can copy structures by simple assignment.
f->a[i] = tmp;
}
Beside the problem with the missing '&' in the memcpy() call
this looks ok (as long as memory for 'f' has been obtained
somewhere in the caller and f->a_len does never get set to
something larger than 32) since f->a[i] now points to memory
you allocated, not memory that is local to this functions (as
it would have been the case if you had assigned the address
of e.g. 'tmpA' to f->a[i]).
Regards, Jens
--
\ Jens Thoms Toerring ___
jt@toerring.de
\__________________________
http://toerring.de