Hi I’m working with C and I have a question about assigning pointers.
struct foo
{
int _bar;
char * _car[SOME_NUMBER]; // this is meant to be an array of char * so that it can hold pointers to names of cars
}
int foofunc (void * arg)
{
int bar;
char * car[SOME_NUMBER];
struct foo * thing = (struct foo *) arg;
bar = thing->_bar; // this works fine
car = thing->_car; // this gives compiler errors of incompatible types in assignment
}
car and _car have same declaration so why am I getting an error about incompatible types? My guess is that it has something to do with them being pointers (because they are pointers to arrays of char *, right?) but I don’t see why that is a problem.
when i declared char * car; instead of char * car[MAXINT]; it compiles fine. but I don’t see how that would be useful to me later when I need to access certain info using index, it would be very annoying to access that info later. in fact, I’m not even sure if I am going about the right way, maybe there is a better way to store a bunch of strings instead of using array of char *?
EDIT: I didn’t mean to use INT_MAX (maximum value of int), it’s just some other int, which is about 20.
You are creating a new array of size MAXINT. I think you want to create a pointer to an array of size MAXINT.
Creating a pointer to an array of char*’s:
The following is an array of size MAXINT to char* elements:
The following is a pointer to: an array of size MAXINT to char* elements:
the following is how you set a pointer to: an array of size MAXINT to char* elements:
Other syntax errors in the question:
foo*notfoo. So it should be:struct foo* thing = (struct foo *) arg;thingnotarg:bar = thing->_bar;car = thing->_car;