Thread: About NULL pointer

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    About NULL pointer

    Hi guys, just flagged in my head something like this, if I have started a pointer to string and write like this statement:
    Code:
     char *p1 = strstr(ac_auto, "Cboard ") + 5
    assuming strstr is defined and all things are alright(ac_auto is a string), but lets assume that's strstr is returning pointer "NULL" so NULL+5 isn't defined because NULL is void and can't be manipulated of. so what's happen in that case? I tried that in my PC the compilation is typically work but on the screen doesn't appear anything however I write printf after this case, meaning
    Code:
     
    char *p1 = strstr(ac_auto, "Cboard ") + 5 ;
    printf("Hi");
    but it doesn't print Hi if strstr returns NULL.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hence, you cannot just add 5 to the result of strstr unless you somehow know in advance strstr will not return a null pointer. Rather, check the return value of strstr first, then if it is not a null pointer add 5 to that result.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    8
    strstr() is a library function that returns the address of the substring. If the substring wasn't found the return value is NULL. you can't add an int-value to NULL. if you try, the program will crash.

    besides: The address of the substring is not the index of that substring.

    example:


    char *ptr = strstr(phrase, "cool");
    printf("%lu", ptr - phrase) // this prints out the index of the substring in the array
    printf("%s", ptr); //this line prints the whole char array beginning with the found index

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ProgrammingJedi
    you can't add an int-value to NULL. if you try, the program will crash.
    Actually, you can an int value to NULL, if NULL is defined to be an integer constant expression with the value of zero. However, NULL can also be such an expression cast to void*, and as you cannot do pointer arithmetic on a void*, you wouldn't be able to add an int value to NULL if it were defined in that way since you would get a compile error. Either way...

    Quote Originally Posted by ProgrammingJedi
    If the substring wasn't found the return value is NULL.
    Not quite: the return value would not be NULL; it would be a null pointer of type char*. Now, you can do pointer arithmetic with a char*, but it would be invalid when that char* is a null pointer, hence your observation that "the program will crash".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Null pointer vs uninitialized pointer
    By Abhishek Kumar in forum C++ Programming
    Replies: 23
    Last Post: 03-05-2014, 12:01 PM
  2. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  3. Null Pointer
    By saswatdash83 in forum C Programming
    Replies: 3
    Last Post: 07-15-2008, 04:12 AM
  4. pointer ot NULL
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2003, 03:43 PM
  5. what is a null pointer?
    By forest in forum C Programming
    Replies: 6
    Last Post: 11-02-2002, 11:02 AM

Tags for this Thread