473,412 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

pointers in function

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[i] = tmp;
}
}
.....
}
Thanks a lot

May 9 '07 #1
6 1303
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
May 9 '07 #2
On 9 May 2007 09:36:04 -0700, 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
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[i] = tmp;
}
}
.....
}
See section 4.8 in the faq (www.c-faq.com)
Remove del for email
May 10 '07 #3
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 Schwarz wrote:
On 9 May 2007 09:36:04 -0700, 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

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[i] = tmp;
}
}
.....
}

See section 4.8 in the faq (www.c-faq.com)
Remove del for email
May 10 '07 #4
sorry for the last post
I got what you meant

Barry Schwarz wrote:
On 9 May 2007 09:36:04 -0700, 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

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[i] = tmp;
}
}
.....
}

See section 4.8 in the faq (www.c-faq.com)
Remove del for email
May 10 '07 #5
qi*****@gmail.com writes:
But I have one more question

how to claim one malloc as static.
What do you mean by that?
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
May 10 '07 #6
qi*****@gmail.com wrote:
sorry for the last post
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
May 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
1
by: ketema | last post by:
Hello, I was wondering if someone could help me with a function I am trying to write. The purpose of the function is to read in text from a file in the following format: FIRSTNAME LASTNAME...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
7
by: Eli Luong | last post by:
Hi, Sorry if I accidentally posted a blank post before. But, I was wondering, I'm practicing with pointers right now, and wanted to know if I am using it correctly in the following code. By...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
19
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.