reading a structure with pointer

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.
 
B

Ben Bacarisse

romesh said:
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.

The code you posted has syntax errors. It is much better to post real
code.
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; */

struct s2 *m6; would be more usual then. You give your self an extra
problem by making this an array of pointers.
}
int f1 (void *par){

You treat par as a pointer to a struct s3. You should declare it as
one. If there is a reason you can't (given that this is not real
code it is hard to say) it is much clearer to start off by making one:

struct s3 *s3p = par;
struct s1 a1;
int no, i;
char a[5];

no =10;
(*(struct s3 *)par).m6 = malloc(sizeof(struct s2 *) *
no);

I would the write this as:

s3p->m6 = malloc(no * sizeof *s3p->m6);
for (i=0; i<no; ++i)
{
strcpy(a, "");

There is no need for this.
sprintf(a, "Hi%d", i);

Personally, I'd sprintf directly into the allocated struct. You gain
little by putting it into a and then copying it.
(*(struct s3 *)par).m6 = malloc(sizeof(struct s2 ) );


s3p->m6 = malloc(sizeof s3p->m6);
memcpy(&(*(struct s3 *)par).m6.m3, a, 4); /* trying to
cope Hi+i into each m3 of m6 */


Yuck! (Sorry, can't help it). Since m6 is a pointer, you can't
use .m3 to access a member. You must use -> instead. Why 4 not 5?
Even better, use sizeof so you copy as much as is declared. Why
memcpy rather than strcpy? strcpy may be better in general if you
have longer strings that are not always full-length.
}
return no;
}
int main(){

int main(void) is better.
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 */


You need to use -> or take my original suggestion an make m6 a pointer
to an array rather than a pointer to an array of pointers.
 
M

Martin Ambuhl

Jujitsu said:
You know, I agree with you.

In fact, there should probably be a rule for this group that any code
posted has to be a complete program that will compile using "gcc
filename.c".

That is an unreasonable position, since many people post questions
precisely because their programs do not compile.

Still, those people need to post complete programs which are as small as
possible while exhibiting their problem.
 
M

Mark L Pappin

Jujitsu Lizard said:
Ben Bacarisse said:
romesh said:
I want to [...]
and that is where I am getting problems.

The code you posted has syntax errors. It is much better to post
real code.

You know, I agree with you.

Fine so far.
In fact, there should probably be a rule for this group

No there should not be.
that any code posted has to be a complete program that will compile
using "gcc filename.c".

Yet another missing of The Point, in several parts:

- If the OP complains that code runs but "doesn't work" (FSVO), then
posting code that has syntax errors is pointless, since we obviously
aren't being shown the code that "doesn't work" as originally
described.

- If the OP's problem is that attempting to compile gives error
messages but s/he doesn't understand them, then posting code with
errors is fine (as long as the actual text of the error messages is
also posted).

- The result of "gcc filename.c" is irrelevant here unless you get the
exact same result with (at least) "gcc -ansi -pedantic filename.c",
since by default gcc is not a C compiler.

mlp
 
G

Guest

Jujitsu Lizard said:
Ben Bacarisse said:
I want to [...]
and that is where I am getting problems.
The code you posted has syntax errors.  It is much better to post
real code.
You know, I agree with you.

Fine so far.

me too
No there should not be.

quite. It's nice if people post complete code. But
sometimes a fragment is enough. Sometimes they are newbies
and don't know better

this is just nuts! Not every has gcc handy. Or wants it.
The PC I'm on at the moment doesn't have gcc.
Yes I could download but why should I
Yet another missing of The Point, in several parts:

- If the OP complains that code runs but "doesn't work" (FSVO), then
posting code that has syntax errors is pointless, since we obviously
aren't being shown the code that "doesn't work" as originally
described.

I assumed he wasn't meaning in this case
- If the OP's problem is that attempting to compile gives error
messages but s/he doesn't understand them, then posting code with
errors is fine (as long as the actual text of the error messages is
also posted).

- The result of "gcc filename.c" is irrelevant here unless you get the
exact same result with (at least) "gcc -ansi -pedantic filename.c",
since by default gcc is not a C compiler.

All World Isn't A VAX
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top