I have a question about type conversion in C.
So, I’m using these lines of code to initialize my index values to NULL:
frameTable[i].lv1Index = (int) NULL;
frameTable[i].lv2Index = (int) NULL;
where frameTable is constructed of the following:
typedef struct {
int lv2Index, lv1Index;
} Frame;
Frame* frameTable;
Can anyone tell me what is wrong with this?
This is my memory allocation:
frameTable = (Frame*) malloc(frameTableSize * sizeof(Frame));
This will probably compile and execute correctly since you’re casting NULL to int (i.e., the compiler assumes you know what you’re doing), but NULL is intended to be used with pointers. Since your structure fields are ints, just set them equal to zero to initialize them (“frameTable[i].lv1Index = 0;”). If you want to indicate that they are not valid indices yet, then set them to -1 or some other invalid value.