Thread: NULL operator

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    NULL operator

    why can't we use in pointers *p=0? we should put instead of 0 another expression that express to 0 which is NULL, is NULL defined in C itself as nothing like '/0'?, meaning if I write for example
    Code:
    a[5]={3,4,5,6,7}
    if a[6]=='NULL' 
         printf("the elements of the array is finished")
    thanks in advance.

  2. #2
    Banned
    Join Date
    Apr 2015
    Posts
    596
    More over, when do we use typedef ? and what does it stand for?

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    nul '\0' and NULL are very different.
    "One L is an ASCII key, two L's point to nothing"

    FAQ > NULL, 0, \0 and nul? - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Wowowow, one step at a time.

    The reason you can't(implicitly) terminate an array with a "NULL" element is because C is really direct about its types. An int is an int, a float is a float, a pointer to struct is a pointer to struct. Nothing less, nothing more.

    In order to do what you want, which is possible with C but not preferable, you'll have to declare the array as an array of pointers. That way, the last one could be NULL.

    Now, typedef is used to define a new type, as the name implies. It's mostly used to shorten or customize other types/structs:
    Code:
    typedef unsigned char BYTE;
    typedef struct
    {
        int value;
    }foo;
    
    BYTE aByte = 255;
    foo aFoo = { 123456 };
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    FAQ > NULL, 0, \0 and nul? - Cprogramming.com
    http://c-faq.com/null/nullor0.html

    I know you just posted an example, but accessing "a[6]" in your array brings you to the land of undefined behavior. The only valid indices of "a[5]" are 0 through 4.

    Also, NULL should not be enclosed in single quotes.

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: 3
    Last Post: 12-09-2008, 11:19 AM
  3. Replies: 1
    Last Post: 07-07-2008, 03:38 AM
  4. Replies: 9
    Last Post: 10-20-2007, 01:05 AM
  5. 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