-1

I'm using an if function that needs to check if the string is a NULL but i don't know how to ask for that.

I know that if(a == 'NULL') \\int a should check to see if the int is NULL but how do I ask if a string is empty or null. I want to say if(s1[0] == NULL) \\char s1[] or something similar to this?

I want to make a code like this?

char s2[10];
int a;

(...)

if(s2[0] == NULL) \\what I am asking about
  printf("null or empty string\n");
else
  printf("string with items\n");

I would like to know what I should put for the if statement to ask if the string is NULL.

  • Seeing as how my last 2 questions did not go well (I blame myself), I would like to start asking shorter and more specific questions in hopes that others will easily understand my problems and provide me a better answer. – mario segale Nov 25 '19 at 17:20
  • 2
    **What?** Are you sure you're talking about C? – Marco Bonelli Nov 25 '19 at 17:21
  • if(int a == 'NULL') will not work. Please learn how to use the debugger and look at the contents of a variable. What is a string internally? – john elemans Nov 25 '19 at 17:21
  • 1
    Neither of your expressions makes much sense. Can you please provide a greater context? Are you referring to a `NULL` string pointer, or to a 'nul' string terminator? – Weather Vane Nov 25 '19 at 17:21
  • The examples you're showing are definitely not valid C. – Marco Bonelli Nov 25 '19 at 17:21
  • Sorry that my question came out wrong. I wanted to show what variables these are but I didn't think to use the double slashes. Also, these are just regular string pointers – mario segale Nov 25 '19 at 17:27
  • The question still doesn't make sense. Show a [MCVE] containing code that compiles. – Marco Bonelli Nov 25 '19 at 17:38
  • Does `int a = 'NULL';` do what you think it does? Output of `printf("%d %X\n", a, a);` is `1314212940 4E554C4C`. – Weather Vane Nov 25 '19 at 17:41

2 Answers2

1
int checkIfNullOrEmpty(const char *str)
{
    return !str || !str[0];
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

NULL is defined as zero or 0.

if( a == 'NULL' )

a is integer. 'NULL' is constant char value of 'NULL'. this is differ from NULL. be careful.

So, You are comparing int with char.

if( s1 == NULL ) Maybe s1 is char * or char[].

string s1 This is not C. string is C++ STL class as I know. it can't be null because it is a class instance itself.

string *s1 can be NULL, because it is a pointer to class instance.

Most case pointer inited as NULL when the pointer is not allocated or nowhere. So, this test if s1 is allocated or not.

if( s1[0] == NULL )

You check if s1 has some string data or empty string "", because C string terminated with char value 0.

Peter Lee
  • 136
  • 9
  • What is this: `'NULL' ` – 0___________ Nov 25 '19 at 17:55
  • 1
    `if( s1[0] == NULL )` NO NO NO. If si is NULL it is undefined behavior – 0___________ Nov 25 '19 at 17:56
  • there are no classes and instances in C – 0___________ Nov 25 '19 at 17:56
  • @FedericoklezCulloca `NULL` is always equal to `0` according to the C standard. – Marco Bonelli Nov 25 '19 at 18:01
  • ok so I know that String s1 does not work in C, and I know that NULL is for int variables and 'NULL' is for characters so would my answer be `if(s1[0] == 'NULL')` with s1 being a char array? – mario segale Nov 25 '19 at 18:04
  • @mariosegale a character constant of more than one character does not make saense in the first place. `'NULL'` does not make sense. `'x'` does. `'\0'` does. – Marco Bonelli Nov 25 '19 at 18:05
  • @MarcoBonelli so it would be `if(s1[0] == '\0')` – mario segale Nov 25 '19 at 18:06
  • @MarcoBonelli So I've tested '\0' and it works. Thank you for answering my question and thank you everyone else for helping as well. I apologize for making this more confusing than it should have been – mario segale Nov 25 '19 at 18:22
  • The Question is modified significantly after answer was written.. – Peter Lee Nov 25 '19 at 18:59
  • @MarcoBonelli both the C99 and C11 standards say that `NULL` is an implementation defined null-pointer constant, so it's not *guaranteed* to be 0. – Federico klez Culloca Nov 25 '19 at 19:20
  • 1
    @FedericoklezCulloca no they don't, see [C99 6.3.2.3 point 3](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) and [C11 6.3.2.3 point 3](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). In particular the two footnotes (55 and 66 respectively), which clearly state that **`NULL` is `0`**. — Quoting: *"An integer constant expression with the value `0`, or such an expression cast to type `void *`,is called a null pointer constant [...] The macro `NULL` is defined in `` (and other headers) as a null pointer constant"*. – Marco Bonelli Nov 25 '19 at 20:16
  • @MarcoBonelli yes, but then the referenced note (7.17 in c99 and 7.19 for c11) says that `NULL` is implementation-defined. So which is which? I'm not saying you're wrong, I'm saying I'm confused. – Federico klez Culloca Nov 25 '19 at 21:04
  • 1
    @FedericoklezCulloca 7.17 just means that a particular implementation gets to choose which form of `0` to use and whether to use a `void *` cast or not. The value is still `0`. See [this answer](https://stackoverflow.com/a/2599766/3889449). – Marco Bonelli Nov 25 '19 at 21:10
  • @MarcoBonelli "regardless of whether or not the machine's internal representation of that has a value of zero" I'm afraid I was thinking of this when I made that first comment. Thanks for setting it straight. – Federico klez Culloca Nov 25 '19 at 22:29
  • @FedericoklezCulloca no problem :) – Marco Bonelli Nov 26 '19 at 00:59