about this source code:
#include <stdio.h>
#include <stdlib.h>
typedef struct foo_str{
char foo_buffer[10];
int foo_int;
}foo_t;
int main(int argc, char **argv)
{
foo_t foo = {0};
foo.foo_int = foo.foo_int;
puts(“hello\n”);
exit(0);
}
if you compile with qcc -Wall -o foo foo.c, you get:
foo.c: In function main': foo.c:11: warning: missing braces around initializer foo.c:11: warning: (near initialization for
foo.foo_buffer’)
Now, if you just change the order of the fields in the structure (which is not always possible!!) like that:
typedef struct foo_str{
int foo_int;
char foo_buffer[10];
}foo_t;
everything is ok. Strange no ?
Alain.
It’s because you are trying to initialise foo_buffer,
which is an array, with 0.
You need to put parentheses around the 0 for this
to compile with no warnings.
Alain Bonnefoy <alain.bonnefoy@icbt.com> wrote:
about this source code:
#include <stdio.h
#include <stdlib.h
typedef struct foo_str{
char foo_buffer[10];
int foo_int;
}foo_t;
int main(int argc, char **argv)
{
foo_t foo = {0};
foo.foo_int = foo.foo_int;
puts(“hello\n”);
exit(0);
}
if you compile with qcc -Wall -o foo foo.c, you get:
foo.c: In function main': foo.c:11: warning: missing braces around initializer foo.c:11: warning: (near initialization for
foo.foo_buffer’)
Now, if you just change the order of the fields in the structure (which is not always possible!!) like that:
typedef struct foo_str{
int foo_int;
char foo_buffer[10];
}foo_t;
everything is ok. Strange no ?
Alain.
–
cburgess@qnx.com
Previously, Colin Burgess wrote in qdn.public.qnxrtp.devtools:
It’s because you are trying to initialise foo_buffer,
which is an array, with 0.
You need to put parentheses around the 0 for this
to compile with no warnings.
Alain Bonnefoy <> alain.bonnefoy@icbt.com> > wrote:
about this source code:
#include <stdio.h
#include <stdlib.h
typedef struct foo_str{
char foo_buffer[10];
int foo_int;
}foo_t;
int main(int argc, char **argv)
{
foo_t foo = {0};
foo.foo_int = foo.foo_int;
puts(“hello\n”);
exit(0);
}
if you compile with qcc -Wall -o foo foo.c, you get:
foo.c: In function main': foo.c:11: warning: missing braces around initializer foo.c:11: warning: (near initialization for
foo.foo_buffer’)
Now, if you just change the order of the fields in the structure (which is not always possible!!) like that:
typedef struct foo_str{
int foo_int;
char foo_buffer[10];
}foo_t;
everything is ok. Strange no ?
Alain.
–
cburgess@qnx.com
Ok but what I find strange is that I don’t get any warning in the second case, i.e. if the struct begins with a simple type, not an array.
Ok but what I find strange is that I don’t get any warning in the second case, i.e. if the struct begins with a simple type, not an array.
This is because gcc knows how to correctly 0 fill the rest of the array.
When it’s a simple integer that you are setting to 0, your syntax was
correct.
–
cburgess@qnx.com