pointers in function

Q

qianz99

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



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 use malloc in the function to evaluate F, will it cause any problem?
will the point link lost when I exit this function?
void evluate_F(F_t *f, ...)
{
char *tmp;
int i;
A_t tmpA;
f->a_len = 10;
for(i=0;i<;f->a_len;i++)
{
strcpy(tmpA.addr,"1234");
tmpA.len = strlen(tmpA.addr);
if((tmp=malloc(sizeof(A_t)))!=NULL)
{
memcpy(tmp,tmpA,sizeof(tmpA));
f->a = tmp;
}
}
.....
}
Thanks a lot
 
J

Jens Thoms Toerring

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 = 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 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).
Regards, Jens
 
B

Barry Schwarz

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
snip

I have thinking of writing a function to evaluate struct F, and when I
eveluate struct H, I will call this function.

I use malloc in the function to evaluate F, will it cause any problem?
will the point link lost when I exit this function?
void evluate_F(F_t *f, ...)
{
char *tmp;
int i;
A_t tmpA;
f->a_len = 10;
for(i=0;i<;f->a_len;i++)
{
strcpy(tmpA.addr,"1234");
tmpA.len = strlen(tmpA.addr);
if((tmp=malloc(sizeof(A_t)))!=NULL)
{
memcpy(tmp,tmpA,sizeof(tmpA));
f->a = tmp;
}
}
.....
}


See section 4.8 in the faq (www.c-faq.com)


Remove del for email
 
Q

qianz99

Thanks for the replying!
But I have one more question

how to claim one malloc as static.

I have a for loop/

Thanks again!

Barry said:
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
snip

I have thinking of writing a function to evaluate struct F, and when I
eveluate struct H, I will call this function.

I use malloc in the function to evaluate F, will it cause any problem?
will the point link lost when I exit this function?
void evluate_F(F_t *f, ...)
{
char *tmp;
int i;
A_t tmpA;
f->a_len = 10;
for(i=0;i<;f->a_len;i++)
{
strcpy(tmpA.addr,"1234");
tmpA.len = strlen(tmpA.addr);
if((tmp=malloc(sizeof(A_t)))!=NULL)
{
memcpy(tmp,tmpA,sizeof(tmpA));
f->a = tmp;
}
}
.....
}


See section 4.8 in the faq (www.c-faq.com)


Remove del for email
 
Q

qianz99

sorry for the last post
I got what you meant

Barry said:
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
snip

I have thinking of writing a function to evaluate struct F, and when I
eveluate struct H, I will call this function.

I use malloc in the function to evaluate F, will it cause any problem?
will the point link lost when I exit this function?
void evluate_F(F_t *f, ...)
{
char *tmp;
int i;
A_t tmpA;
f->a_len = 10;
for(i=0;i<;f->a_len;i++)
{
strcpy(tmpA.addr,"1234");
tmpA.len = strlen(tmpA.addr);
if((tmp=malloc(sizeof(A_t)))!=NULL)
{
memcpy(tmp,tmpA,sizeof(tmpA));
f->a = tmp;
}
}
.....
}


See section 4.8 in the faq (www.c-faq.com)


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top