wearyweary
Programmer
- Mar 23, 2001
- 4
Hey guys,
I've come across quite the annoying bug in my code..
I working with a butch of "Classes" I made in C, made up of structures with data and pointers to functions. Everything is working great, but I'm having one problem with dynamically allocating an Array for an object. Heres the problem
typedef struct //define an Object with an array
{
void* anArray[50]; //array of pointers
}arrayObject;
//now we are in main
arrayObject* myArrayObject; //a pointer to an object
myArrayObject = malloc(sizeof(arrayObject)); //make memory
//this line will cause an "incompatible pointer assignment"
myArrayObject->anArray = malloc(sizeof(void*)*50);
I thing I am confused about how an array really works.. I know it should just be a pointer to the begining element of the array, but then there shouldn't be a problem making it point to the beginning of some free memory. Should I use "void** myarray;" instead of the array notation. I believe this crashed when I tryed "myarray[10]=0;"
Anyway, any help, or explaination of extact WHAT is going on when you make an array would be usefull..
Thanks,
Eric
I've come across quite the annoying bug in my code..
I working with a butch of "Classes" I made in C, made up of structures with data and pointers to functions. Everything is working great, but I'm having one problem with dynamically allocating an Array for an object. Heres the problem
typedef struct //define an Object with an array
{
void* anArray[50]; //array of pointers
}arrayObject;
//now we are in main
arrayObject* myArrayObject; //a pointer to an object
myArrayObject = malloc(sizeof(arrayObject)); //make memory
//this line will cause an "incompatible pointer assignment"
myArrayObject->anArray = malloc(sizeof(void*)*50);
I thing I am confused about how an array really works.. I know it should just be a pointer to the begining element of the array, but then there shouldn't be a problem making it point to the beginning of some free memory. Should I use "void** myarray;" instead of the array notation. I believe this crashed when I tryed "myarray[10]=0;"
Anyway, any help, or explaination of extact WHAT is going on when you make an array would be usefull..
Thanks,
Eric