Check if string is null

This is a discussion on Check if string is null within the C Programming forums, part of the General Programming Boards category; Hi, Can you tell me how to check if a string is null in C? I tried p != '

Thread: Check if string is null

' ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Question Check if string is null

    Hi,

    Can you tell me how to check if a string is null in C?

    I tried p != '\0' doent seem to wrk though!

    thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Do you want to test if a character array points to NULL or a string is empty? Your wording doesn't make much sense.

    But the two I said:
    Code:
    p == NULL /* True if character array points to NULL */
    !*p or *p == '\0' /* True if string empty */

  3. #3
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Code:
    char *p=NULL;
    if (!p) printf("p is null");
    else printf("p is not null");
    keep in mind that if i didn't write the assignment (p=NULL) its value would be undefined and the if statement wouldn't work. in a string '\0' is the terminating null byte, you can check when a string is finished but you need to dereference the address:
    Code:
    char *p;
    if (*p=='\0')...
    Hope that helped

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by doubty View Post
    Can you tell me how to check if a string is null in C?

    I tried p != '\0' doent seem to wrk though!
    It all depends on what you mean, and what p is declared as. That looks like an empty string check to me.
    All types of strings in all languages can be "empty" but only some of them can also represent the concept of being "null".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21