2

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));
Jojo Jonas
  • 225
  • 2
  • 4
  • 15
  • see also http://stackoverflow.com/questions/7016861/null-pointer-in-c-and-c about the difference between `NULL` and `0` – DirtY iCE Nov 22 '11 at 23:41

4 Answers4

4

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
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • But 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. – Jojo Jonas Nov 22 '11 at 23:17
  • @JojoJonas: I'm sorry, I don't understand the question. Why would anything interfere with anyone? `frameTable[i]` is the same as `*(frameTable + i)`... – Kerrek SB Nov 22 '11 at 23:19
  • so this is where my indices are being used. if (frameTable[i].lv2Index == (int) NULL) { – Jojo Jonas Nov 22 '11 at 23:21
  • @JojoJonas: I'm sorry, I still have no idea what the problem is. In any event, your code should be `frameTable[i].lv2Index = 0` and `if (frameTable[i].lv2Index == 0)` etc. – Kerrek SB Nov 22 '11 at 23:25
  • I guess I'm not explaining it well. Sorry. – Jojo Jonas Nov 22 '11 at 23:29
  • Someone did suggest using -1's, which will be fine, since I will never have to access frameTable[-1] – Jojo Jonas Nov 22 '11 at 23:30
  • @JojoJonas: Do you understand that you have an **array** of compound structures? – Kerrek SB Nov 22 '11 at 23:31
  • @Jojo, `(int) NULL` will be equal to `0`, so `frameTable[i].lv2Index == (int) NULL` ends up the same as `frameTable[i].lv2Index == 0`. If you want a special `int` value for "invalid index", `-1` would be a better choice. – sth Nov 22 '11 at 23:32
  • Yep, that's what I went with. Thanks guys. – Jojo Jonas Nov 23 '11 at 01:32
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.

Kaolin Fire
  • 2,521
  • 28
  • 43
3

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.

Alex Measday
  • 231
  • 1
  • 2
0

In c, besides pointers you cannot set c objects to NULL. You cannot cast NULL to other object since it encapsulates nothing. So you might want to set your struct variables to 0 instead of NULL.

dodo
  • 109
  • 4