C type conversion between NULL and integer

25,378

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.

Share:
25,378

Related videos on Youtube

Jojo Jonas
Author by

Jojo Jonas

Updated on November 23, 2020

Comments

  • Jojo Jonas
    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));
    
  • Jojo Jonas
    Jojo Jonas over 12 years
    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.
  • Kerrek SB
    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
    Jojo Jonas over 12 years
    so this is where my indices are being used. if (frameTable[i].lv2Index == (int) NULL) {
  • Kerrek SB
    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 and if (frameTable[i].lv2Index == 0) etc.
  • Jojo Jonas
    Jojo Jonas over 12 years
    I guess I'm not explaining it well. Sorry.
  • Jojo Jonas
    Jojo Jonas over 12 years
    Someone did suggest using -1's, which will be fine, since I will never have to access frameTable[-1]
  • Kerrek SB
    Kerrek SB over 12 years
    @JojoJonas: Do you understand that you have an array of compound structures?
  • sth
    sth over 12 years
    @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.
  • Jojo Jonas
    Jojo Jonas over 12 years
    Yep, that's what I went with. Thanks guys.