0
int func(char* string, char from, char to)
{
   int result = 0;
   if(!string) return 0;
// ...
}

i don't know whether this if-statement is checking, that the given string is empty or NULL. I tried to check it but I didn't receive a clear answer.

Sedem
  • 47
  • 1
  • 9
  • check against NULL it is. – Jean-François Fabre Jan 27 '17 at 15:50
  • It's a null check, `if(!string)` is the same as `if(string == 0)` is the same as `if(string == NULL)`. – George Jan 27 '17 at 15:50
  • but i wrote if(!string) return 5; and i gave an empty string, why did the function not return 5? it returned 0. – Sedem Jan 27 '17 at 15:51
  • Just to amplfy, it is NOT checking for an empty string. A literal empty string will have an address, and therefore is not NULL – infixed Jan 27 '17 at 15:52
  • how should the given string look like to receive 5 if my return is 5 for a NULL string? – Sedem Jan 27 '17 at 15:54
  • 1
    `char* string = NULL;` <-- declare and initalise a pointer to NULL. `char* string = "";` <-- declare and initialise a pointer to a string literal containing a nul character. – George Jan 27 '17 at 15:55
  • 1
    It would be better to think of `char* string` as a pointer to a string, than the string itself. but you can always change your `return 0;` to a `return 5;` – infixed Jan 27 '17 at 15:56
  • @Sedem It will not return 5 as you expected. That check is precisely for string NULL check. To find whether the string is empty, I've posted ny answer below. You can do either via strlen() or just *derefernce the first char of your string and comapare it with NULL as empty strings have string[0] = '\0'. – user7375520 Jan 27 '17 at 16:14
  • 1
    Keep safety checks in mind with @user7375520 s answer, if you think there's a chance `string` will be null, null check it, and if you're using `strlen` make sure the string has a null terminator else your code might summon [time travelling coke addicts](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points). – George Jan 27 '17 at 16:20
  • @George Definitely! strlen() should only be used when the string is Null terminated. Otherwise use the second way . – user7375520 Jan 27 '17 at 16:23

3 Answers3

1

Your code checks whether string is NULL. If so, it returns 0. To check whether string is empty, you could do either of the following:

if (strlen(string) == 0)
{
    printf("String is empty.\n");
    return -1;
}

or you can do:

if (*string == '\0')
{
    printf("String is empty.\n");
    return -1;
}

strlen() way should only be used when the string is NULL terminated.

user7375520
  • 273
  • 2
  • 15
1
if (!string)

is equivalent to:

if (string==0)

and so it tests whether the parameter string that is passed is pointing to an object. Whether that object is a string (a sequence of characters terminated with a null character) it cannot check.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

It is a bit confusing as logically it say 'if not string' but instead of it meaning if this is not a string it actually means if this string doesn't exist ie if it is null.

Alex
  • 419
  • 6
  • 24