Thread: int NULL

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    16

    int NULL

    Is there anyway to int = NULL; perform more like ch = NULL;?

    I am supposed to "delete" entries in a file that contains struct values (one int and one char), but when you NULL the int part of the struct and print the new value it shows 0, not nothing in it's place. (which is what I need to do)

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Code:
    if (myint == 0) /* nothing */;
    else printf("%d", myint);
    Voila!

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    16
    Well I am overwriting the data in a file with new information, which I mean technically it would work to the viewer of the file (though would assume the information is gone even though the data is still there, just overlooked)

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Not sure I entirely understand, but an int can not contain "nothing". NULL actually is basically the same thing as 0. The difference is that when you assign a null character to the beginning of a character array, functions that treat the character array as a string will only work with data before the null character - in that case "nothing". But what you're really doing behind the scenes is assining a byte with the value '0' to the first element of the array.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    16
    Yea that is what I figured, I was just wondering if there was a roundabout way to do it but I suppose there isn't! Thank's though!

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    NULL should not be assigned to an int (or any other integer type); NULL is strictly for pointers. It is not for characters, either (which are really just integers).

    If you have an int value that can be “invalid”, you can pick a value that the int will never take on (e.g. -1 if the values are all positive) and check for -1 before printing.

    You can also use a companion boolean-style object that keeps track of whether the value is valid.

    Examples:
    Code:
    /* first example */
    int val = get_value();
    if(invalid) val = -1;
    if(val != -1) printf("%d\n", val);
    
    /* second example */
    int val = get_value();
    int has_value = 1;
    if(invalid) has_value = 0;
    if(has_value) printf("%d\n", val);

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    NULL vs. NUL
    "Two L's points to no thing
    One L is the end of an ASCII string
    "

    Looking at the definition of the pointer NULL in my version of CodeBlocks
    Code:
    #define NULL ((void *)0)
    You should use '\0' for NUL, because the ASCII value of NUL is not defined by default.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    Last edited by Click_here; 11-27-2012 at 05:57 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign null value to a struct and check if it is null
    By ymc1g11 in forum C Programming
    Replies: 10
    Last Post: 11-01-2012, 03:58 PM
  2. Replies: 9
    Last Post: 10-20-2007, 01:05 AM
  3. accept(ListenSocket, NULL, NULL); cause program to hang?
    By draggy in forum Networking/Device Communication
    Replies: 11
    Last Post: 06-16-2006, 03:40 PM
  4. What does NULL mean?
    By Sentral in forum C++ Programming
    Replies: 19
    Last Post: 10-29-2005, 11:22 PM
  5. NULL & int*
    By rotis23 in forum C Programming
    Replies: 2
    Last Post: 06-16-2003, 08:06 AM