views:

44

answers:

1

This does work

LLIST *mylist[10] = {NULL}; 

But would if I wanted to do this I get errors:

int x=10; 

LLIST *mylist[x] = {NULL};

x can be any value I'm setting it to 10 for the time being. x is going to be used as a counter.

+3  A: 

Support for variable length arrays (which your second example uses) came in the C99 standard, which your compiler may not support.

For gcc, you should pass the -std=c99 option when compiling.

Kyle Lutz
I use a make file that i run to compile
MRP
In that case you should modify the `CFLAGS` and add `-std=c99`.
Kyle Lutz
@MRP: Then set the CFLAGS environment variable to include `-std=c99`
rampion
I'm new using c and compiling
MRP
ok i will try that
MRP
here is my make fileCC=gccCFLAGS=-Wall -g#LIBS=-lreadline -lmOBJS=llist.o myprogram.oINCLUDES=llist.hall: $(OBJS) $(CC) $(CFLAGS) $(OBJS) $(LIBS) -o myprogram.c.o: $*.h $(INCLUDES) $(CC) $(CFLAGS) -c $*.cclean: rm -f *.o myprogram
MRP
i wish there was a better way to post it
MRP
Would i just add then CFLAGS=-Wall -g -std=c99
MRP
Yupp, it works right? Or do you still get the same compiler errors?
emil
yeah I get the same error
MRP
error: variable-sized object may not be initialized
MRP
and that means, the compiler don't know how long it should initialize your array at _compile time_, then you will need to do#include <strings.h>...bzero(mylist, sizeof(LLIST *)*x);
emil
i have #include <strings.n> i never heard of the bzero function.. My original thought when starting this program it would be easier doing a link list of link lists.. defining an array size of an arbitrary number makes no sense.
MRP
Is this then the correct way to implement it:LLIST *mylist[] = {NULL};bzero(mylist, sizeof(LLIST *)*x);
MRP
it does compile soo thats good news I just dont know if it works the same
MRP
LLIST *mylist[] = {NULL}; is the same as writing LLIST *mylist[1] = {NULL};you should change LLIST *mylist[] = {NULL}; to LLIST *mylist[x]; and then do your bzero. otherwise you will have to allocate the array yourself with ex. malloc(3)
emil
so I have... int x; LLIST *mylist[x]; x=10; then bzero(mylist, sizeof(LLIST *)*x);
MRP
im still not getting it to work.
MRP
int x = 10; LLIST *mylist[x]; then bzero(mylist, sizeof(LLIST *)*x);Otherwise the x will have a random value when it is to decide the size of the mylist array.
emil