C type conversion between NULL and integer
Solution 1
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.
Solution 2
NULL
is a macro that represents the null pointer, not an integer. What you're doing is one of the most widespread and painful abuses that is causing the C++ standardisers no end of headache.
If you want an integer to be zero, use the octal 0
literal:
int n = 0;
Next, your code is fine, but missing the allocation. Where are the frameTable[i]
variables stored?
You need one of the following two:
Frame frameTable[2]; // automatic storage
Frame * frameTable = malloc(sizeof(Frame) * 2); // manual, your responsibility now
Solution 3
NULL is for pointers, not ints. While you can force the casting of just about anything, you're better off being more explicit and exact in setting them to 0.
You can also get the same effect by calloc'ing the memory (versus mallocing it) when you allocate your frameTable(s). Calloc clears all bytes to 0 in the memory that it allocates.
Related videos on Youtube
Jojo Jonas
Updated on November 23, 2020Comments
-
Jojo Jonas over 3 years
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));
-
DirtY iCE over 12 yearssee also stackoverflow.com/questions/7016861/null-pointer-in-c-and-c about the difference between
NULL
and0
-
-
Jojo Jonas over 12 yearsBut being that these are indices, wouldn't their being initialized to 0 interfere with the indexing? I will be checking their values, a 0 could potentially mess with that first element.
-
Kerrek SB over 12 years@JojoJonas: I'm sorry, I don't understand the question. Why would anything interfere with anyone?
frameTable[i]
is the same as*(frameTable + i)
... -
Jojo Jonas over 12 yearsso this is where my indices are being used. if (frameTable[i].lv2Index == (int) NULL) {
-
Kerrek SB over 12 years@JojoJonas: I'm sorry, I still have no idea what the problem is. In any event, your code should be
frameTable[i].lv2Index = 0
andif (frameTable[i].lv2Index == 0)
etc. -
Jojo Jonas over 12 yearsI guess I'm not explaining it well. Sorry.
-
Jojo Jonas over 12 yearsSomeone did suggest using -1's, which will be fine, since I will never have to access frameTable[-1]
-
Kerrek SB over 12 years@JojoJonas: Do you understand that you have an array of compound structures?
-
sth over 12 years@Jojo,
(int) NULL
will be equal to0
, soframeTable[i].lv2Index == (int) NULL
ends up the same asframeTable[i].lv2Index == 0
. If you want a specialint
value for "invalid index",-1
would be a better choice. -
Jojo Jonas over 12 yearsYep, that's what I went with. Thanks guys.