I’m working on a program and can’t understand the compiler errors.
arrays.c:12:21: error: flexible array member not at end of struct
arrays.c: In function 'insert':
arrays.c:22:4: warning: statement with no effect
I know these are very simple things, but I cant fix them. I am not sure if I my insert function is correct. Can anyone please help me? Thanks
#include <stdio.h>
#define AMOUNT 7
return 0;
}
A struct member declared with empty square brackets
[]is called a flexible array member. This is explained in the comp.lang.c FAQ, question 2.6 (scroll all the way to the bottom). A flexible array member may appear only as the last declared member of a struct, which is what your compiler is complaining about.But moving it to the end of the struct is not the best solution; you probably don’t want to use a flexible array member at all. (You could, but it introduces unnecessary complexity.)
As dasblinkenlight says, you can make the
namemember a pointer:namewould then point to a string representing the person’s name — which means you’ll need to allocate memory to hold the string.Or you can make
namean array ofchar, with a fixed maximum size:You can then use the
strcpyfunction to copy a value intoname— being careful that you don’t try to copy more thanMAX_NAMEbytes.In your
insertfunction, these statements:were presumably intended to initialize the
namesandagesmembers (better names:nameandage), but in fact they do nothing; they refer to those members, but they don’t do anything with them. Forage, you can simply assign the value of theageparameter (which you’re currently ignoring) to theagemember:The way to assign a value to
namedepends on whether you decide to define it as a pointer or as an array.