-1

Why did this not print an assert error?

This is my code:

#include<assert.h>
#include<stdio.h>

int main(){
    int n =12;
    char str[50] = "";
    assert(n>=10);
    printf("output :%d\n",n);
    assert(str!=NULL);
    printf("output :%s\n",str);
}
digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • 12
    Why would you expect `str` to be `NULL`? – Nathan Pierson Jul 24 '22 at 16:18
  • 3
    Arrays *decay* to pointers to their first element, so `str` will decay to `&str[0]`. And since arrays can't be empty, all pointers to elements in the array will be valid and definitely non-null. – Some programmer dude Jul 24 '22 at 16:19
  • 4
    Did you mistake this check for `assert(str[0] != '\0');`? – fabian Jul 24 '22 at 16:20
  • 4
    My ***guess*** is that you confuse the null *pointer* with the null *string terminator character*. They are two different things. `NULL` is a null pointer, while `'\0'` is a string null terminator character. To check if a character is equal to the terminator you need to get that character, like `str[0]`. – Some programmer dude Jul 24 '22 at 16:20
  • 2
    Also, there's *nothing* in the show code that is C++ specific. It's a plain C program. – Some programmer dude Jul 24 '22 at 16:21
  • 1
    Question is tagged as `C++`, but code is valid `C` code (it is not idiomatic `C++`, but compiles fine in `C++`)! – Marek R Jul 24 '22 at 16:24
  • @Someprogrammerdude On one hand yes, but on the other we generally want to know if OP uses a C++ *compiler* or a C *compiler*, so the `c++` tag is correct (not sure if you were arguing with that or not). – HolyBlackCat Jul 24 '22 at 16:24
  • I compile it with C and C++ compiler and still no error, and when i edit it like fabian's comment it still no error – noobprogramer Jul 24 '22 at 16:29
  • Is that so? When I edit it like fabian suggested I do get an error https://godbolt.org/z/EfMoPsszW – StoryTeller - Unslander Monica Jul 24 '22 at 16:39

2 Answers2

1
char str[50] = "";

This makes str into an array of 50 chars, and initialises the memory to all zeroes (first byte explicitly from "" and rest implicitly, because C does not support partial initialisation of arrays or structs).


assert(str!=NULL);

When used in an expression, array is treated as pointer to its first element. The first element of the array very much has an address, so it is not NULL.


If you want to test if the first element of the array is 0, meaning empty string you need

assert(str[0] != '\0');

You could compare to 0, or just say assert(*str);, but comparing to character literal '\0' makes it explicit to the reader of the code, that you are probably testing for string-terminating zero byte, not some other kind of zero, even if for the C compiler they're all the same.

hyde
  • 60,639
  • 21
  • 115
  • 176
-1

str is an array which is effectively a pointer, which means the address in memory where the array is stored. A pointer is NULL if it doesn't point to any specific place in memory. Your str pointer is not null, it points to a place in memory containing an array of 50 elements.

That array is an array of chars, otherwise known as a string. You initialise it to an empty string but it's still there and the pointer to it (str) is still a valid, non-NULL pointer.

str!=NULL does not mean "str is not an empty string"! It means "str is not an undefined pointer".

pdp8
  • 206
  • 1
  • 6
  • 3
    *"which is effectively a pointer"* - No, no, most definitely not. This is the most misleading falsehood novices are taught, and it should be challenged at every turn. – StoryTeller - Unslander Monica Jul 24 '22 at 16:41
  • [An array will decay to a pointer](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) in most situations, but I disagree with your statement that it "effectively" is a pointer. For example, using the `sizeof` operator on an array has a very different effect than using that operator on a pointer. – Andreas Wenzel Jul 24 '22 at 19:42
  • Array types and pointer types are different things. However, array is effectively treated as a pointer except in a handful of situations, like sizeof(). This is described here: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf That's why you can use pointer aritmetic on an array. This may also help: http://www.cs.ecu.edu/karl/3300/spr16/Notes/C/Array/pointer.html#:~:text=An%20array%20is%20considered%20to,issue%20of%20using%20an%20array. – pdp8 Jul 25 '22 at 14:06