E
elmafiacs
I need some help with an error I'm getting...
Sorry if this is basic stuff, but I'm still a newbie on C programming
(working up, though).
Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff).
Sorry about the long subject, I didn't know how to explain it better...
struct small {
int num;
};
struct big {
struct small *small_thing;
};
void f(struct small **s)
{
s = malloc(sizeof(struct small));
memset(s, 0, sizeof(struct small));
s->num = 42;
}
main()
{
struct big *big_thing;
big_thing = malloc(sizeof(struct big));
memset(&big_thing, 0, sizeof(struct big));
f(&big_thing->small_thing);
printf("%i\n", big_thing->small_thing->n);
}
But when I do:
s->num = 42;
the compiler fails with error 'request for member 'num' in something
not an structure or union', and I don't know why.
I tried this way:
void f(struct small **s)
{
struct small *s_ptr;
s_ptr->num = 42;
memcpy(s, &s_ptr, sizeof(struct small));
}
I'm not sure, but I think that the second function is unnecessary. So,
how to fix the first function?
Thanks.
Sorry if this is basic stuff, but I'm still a newbie on C programming
(working up, though).
Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff).
Sorry about the long subject, I didn't know how to explain it better...
struct small {
int num;
};
struct big {
struct small *small_thing;
};
void f(struct small **s)
{
s = malloc(sizeof(struct small));
memset(s, 0, sizeof(struct small));
s->num = 42;
}
main()
{
struct big *big_thing;
big_thing = malloc(sizeof(struct big));
memset(&big_thing, 0, sizeof(struct big));
f(&big_thing->small_thing);
printf("%i\n", big_thing->small_thing->n);
}
But when I do:
s->num = 42;
the compiler fails with error 'request for member 'num' in something
not an structure or union', and I don't know why.
I tried this way:
void f(struct small **s)
{
struct small *s_ptr;
s_ptr->num = 42;
memcpy(s, &s_ptr, sizeof(struct small));
}
I'm not sure, but I think that the second function is unnecessary. So,
how to fix the first function?
Thanks.