C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-04-2009, 09:27 AM   #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
doubty is offline   Reply With Quote
Old 07-04-2009, 09:35 AM   #2
Registered User
 
Join Date: Oct 2008
Posts: 452
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 */
EVOEx is offline   Reply With Quote
Old 07-04-2009, 09:41 AM   #3
gcc -Wall -pedantic *.c
 
Join Date: Jan 2009
Location: London
Posts: 52
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
flexo87 is offline   Reply With Quote
Old 07-04-2009, 03:10 PM   #4
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
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
iMalc is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP Question DB Access Wrapper Classes digioz C# Programming 2 09-07-2008 04:30 PM
String issues The_professor C++ Programming 7 06-12-2007 09:11 AM
String editor for a sentence inputted by a user - any suggestions or ideas? the_newbug C Programming 4 03-03-2006 02:11 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
lvp string... Magma C++ Programming 4 02-27-2003 12:03 AM


All times are GMT -6. The time now is 07:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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