A string literal is an array of characters* (with static storage), which contains all the characters in the literal along with a terminator. The size of an array is the size of the element multiplied by the number of elements in the array.

The literal "" is an array that consists of one char with the value 0. The type is char[1], and sizeof(char) is always one; thereforesizeof(char[1]) is always one.

In C, NULL is implementation-defined, and is often ((void*)0). The size of a void*, on your particular implementation, is 4. It may be a different number depending on the platform you run on. NULL may also expand to an integer of some type of the value 0, and you'd get the size of that instead.

*A literal is not a pointer, arrays are not pointers, pointers do not play a role in this part of the question.

Answer from GManNickG on Stack Overflow
Top answer
1 of 5
21

A string literal is an array of characters* (with static storage), which contains all the characters in the literal along with a terminator. The size of an array is the size of the element multiplied by the number of elements in the array.

The literal "" is an array that consists of one char with the value 0. The type is char[1], and sizeof(char) is always one; thereforesizeof(char[1]) is always one.

In C, NULL is implementation-defined, and is often ((void*)0). The size of a void*, on your particular implementation, is 4. It may be a different number depending on the platform you run on. NULL may also expand to an integer of some type of the value 0, and you'd get the size of that instead.

*A literal is not a pointer, arrays are not pointers, pointers do not play a role in this part of the question.

2 of 5
15

The empty string "" has type char[1], or "array 1 of char". It is not a pointer, as most people believe. It can decay into a pointer, so any time a pointer to char is expected, you can use an array of char instead, and the array will decay into a pointer to its first element.

Since sizeof(char) is 1 (by definition), we therefore have sizeof("") is sizeof(char[1]), which is 1*1 = 1.

In C, NULL is an "implementation-defined null pointer constant" (C99 §7.17.3). A "null pointer constant" is defined to be an integer expression with the value 0, or such an expression cast to type void * (C99 §6.3.2.3.3). So the actual value of sizeof(NULL) is implementation-defined: you might get sizeof(int), or you might get sizeof(void*). On 64-bit systems, you often have sizeof(int) == 4 and sizeof(void*) == 8, which means you can't depend on what sizeof(NULL) is.

Also note that most C implementations define NULL as ((void*)0) (though this is not required by the standard), whereas most C++ implementations just define NULL as a plain 0. This means that the value of sizeof(NULL) can and will change depending on if code is compiled as C or as C++ (for example, code in header files shared between C and C++ source files). So do not depend on sizeof(NULL).

🌐
Quora
quora.com › What-is-size-of-null-in-c-language
What is size of null in c language? - Quora
Answer (1 of 4): A good answer from stackOverflow Question - why sizeof(“”) is equivalent to 1 and sizeof(NULL) is equivalent to 4 in c-language? Ans - A string literal is an array of characters* (with static storage), which contains all ...
🌐
Hacker News
news.ycombinator.com › item
sizeof(NULL) in 4 in C++ in a 64 bit system. sizeof(NULL) is 8 in C in a 64 bit ... | Hacker News
January 30, 2017 - sizeof(NULL) is 8 in C in a 64 bit system · The difference is because NULL in C is a variable of a void pointer type with a value zero. In C++,NULL is a variable with a value zero that gets deduced to an int type
🌐
Reddit
reddit.com › r/c_programming › [need explanation] casting null pointer to get sizeof() struct members
r/C_Programming on Reddit: [Need explanation] casting null pointer to get sizeof() struct members
March 19, 2025 -

In this Stackoverflow post[1] is stumbled upon a 'trick' to get the size of struct members like so: sizeof(((struct*)0)->member) which I struggle to comprehend what's happening here.

what I understand:
- sizeof calculates the size, as normal
- ->member dereferences as usual

what I don't understand:
- (struct*) 0 is a typecast (?) of a nullptr (?) to address 0 (?)

Can someone dissect this syntax and explain in detail what happens under the hood?

[1] https://stackoverflow.com/a/3553321/18918472

Top answer
1 of 5
31
It helps to go back to first principals and build it up. Given a struct: struct foo { int a; }; The problem is sizeof() ONLY works on FULL types or instances not types of member fields. We CAN'T do this to get at a struct's member field ... printf( "sizeof foo : %zu\n", sizeof( struct foo ) ); // OK // printf( "sizeof foo.a: %zu\n", sizeof( struct foo.a ) ); // ERROR ... so we need to use a temporary. struct foo f; printf( "sizeof f.a : %zu\n", sizeof( f.a ) ); // OK We could use a pointer instead, and deference that pointer: struct foo *p = &f; printf( "sizeof p->a : %zu\n", sizeof( p->a ) ); // OK However, the address of the temporary doesn't matter! We could have a pointer where the instance of foo is at address 0! struct foo *q = 0; printf( "sizeof q->a : %zu\n", sizeof( q->a ) ); // OK We can remove the temporary entirely by inlining it -- pretending we have the struct at address 0. printf( "sizeof 0->a : %zu\n", sizeof( ((struct foo*)0)->a) ); // OK We can turn that into a macro to help readability: #define MEMBER_SIZE( type, member ) (sizeof( ((struct type *)0)->member )) printf( "macro foo,a : %zu\n", MEMBER_SIZE(foo,a) ); // OK If you already know the struct type you can avoid passing the type. #define FOO_MEMBER_SIZE(member) (sizeof( ((struct foo *)0)->member )) printf( "macro a : %zu\n", FOO_MEMBER_SIZE(a) ); // OK Demo below: #include struct foo { int a; }; int main() { printf( "sizeof foo : %zu\n", sizeof( struct foo ) ); // OK // printf( "sizeof foo.a: %zu\n", sizeof( struct foo.a ) ); // ERROR struct foo f; printf( "sizeof f.a : %zu\n", sizeof( f.a ) ); // OK struct foo *p = &f; printf( "sizeof p->a : %zu\n", sizeof( p->a ) ); // OK struct foo *q = 0; printf( "sizeof q->a : %zu\n", sizeof( q->a ) ); // OK printf( "sizeof 0->a : %zu\n", sizeof( ((struct foo*)0)->a) ); // OK #define MEMBER_SIZE( type, member ) sizeof( ((struct type*)0)->member ) printf( "macro foo,a : %zu\n", MEMBER_SIZE(foo,a) ); // OK #define FOO_MEMBER_SIZE(member) (sizeof( ((struct foo *)0)->member )) printf( "macro a : %zu\n", FOO_MEMBER_SIZE(a) ); // OK return 0; }
2 of 5
10
sizeof doesn't evaluate the operand unless it's a variable length array. So, the cast and -> are fine as they're not evaluated. After all, the sizeof operator only needs to know the type of the operand to figure out the size. So, there's no point at all in evaluating the value of the operand expression (unless it's a variable length array).
🌐
Reddit
reddit.com › r/c_programming › it's okay to "dereference" a null pointer inside a sizeof(), right?
r/C_Programming on Reddit: It's okay to "dereference" a NULL pointer inside a sizeof(), right?
January 14, 2016 -

I was skimming the Linux coding style guide just now and saw this:

The preferred form for passing a size of a struct is the following:

p = kmalloc(sizeof(*p), ...);

The alternative form where struct name is spelled out hurts readability and introduces an opportunity for a bug when the pointer variable type is changed but the corresponding sizeof that is passed to a memory allocator is not.

When I read that I was like, "oh that makes sense, I'll do that!" but then I thought, wait, what if the pointer has already been initialized to NULL. Is it undefined behavior? My understanding is that sizeof() doesn't actually access the value, so there won't be a dereference. Am I wrong?

🌐
Aticleworld
aticleworld.com › home › what is a null pointer in c/c++?
What is a Null Pointer in C/C++? - Aticleworld
January 25, 2023 - Yes, you can use the sizeof operator on the null pointer. It returns the same size as it returns for other pointers. That means if the pointer size for a platform is 4 bytes, the sizeof() operator on NULL yields 4 bytes.
🌐
Sololearn
sololearn.com › en › Discuss › 2511403 › what-is-the-size-of-null-string-in-c
What is the size of null string in c? | Sololearn: Learn to code for FREE!
Sizeof operator counts null cheatector in string by default null cheatector is present So empty string contain 1 character size is 1
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 312240 › sizeof-ptr-where-ptr-null
c - sizeof(*ptr) where ptr==NULL? | DaniWeb
September 18, 2010 - Given that the statement within the if block is only executed if np is assigned to NULL, I don't understand why you are allowed to dereference np to get to a non-existent struct nlist object before invoking sizeof on it? Thanks for your help. ... As @AncientDragon pointed out, the expression inside sizeof is used only for its type in normal C — it does not perform a runtime dereference or any side‑effects, and the operator yields a size_t value.
Find elsewhere
🌐
Bytes
bytes.com › home › forum › topic
Size of null - Post.Byes
January 9, 2010 - When i did printf("%d",sizeof(NULL)); answer is coming as 2 in Turbo C... Sizeof integer is also coming as 2... We cannot assume that null is of same size as integer.
🌐
Sololearn
sololearn.com › en › Discuss › 3027137 › what-is-the-size-of-null-pointer
What is the size of null pointer?? | Sololearn: Learn to code for FREE!
c · 3rd May 2022, 8:18 AM · Tanaya Tidke · 1 AnswerAnswer · 0 · sizeof("") is equivalent to 1 .and sizeof(NULL) is equivalent to 4 in c · 3rd May 2022, 11:14 AM · 🤔☺️😳😇 · Answer · Learn more efficiently, for free: Introduction ...
🌐
Reddit
reddit.com › r/c_programming › surprising results between sizeof() and strlen when allocating a string with the wrong number of elements
r/C_Programming on Reddit: Surprising results between sizeof() and strlen when allocating a string with the wrong number of elements
October 5, 2019 -

I know that in usual for strings, sizeof() and strlen() return values that differ by 1, because sizeof() is counting the null terminator, whereas strlen() isn't. But I'm trying to understand the discrepancy between sizeof() and strlen() that arises when I allocate a 5-character string into a char[5] array that doesn't have room for the null-terminator.

#include <stdio.h>

#include <string.h>

int main(){

char c[5] = "hello";

char d[] = "hello";

printf("Sizeof is %lu, and strlen is %lu.\n",sizeof(c),strlen(c));

printf("Sizeof is %lu, and strlen is %lu.\n",sizeof(d),strlen(d));

}

Output:

Sizeof is 5, and strlen is 5

Sizeof is 6, and strlen is 5

I find this a bit confusing. If I do char[4] = "hello";, then I get a compiler warning for the "hello" being bigger than char[4]. But in reality, "hello" should also be bigger than char[5] too, right?

The first line reporting sizeof is 5 and strlen is 5 seems to be consistent with the char[5] declaration just omitting the null-terminator, but the fact that strlen() in the second line is just returning 5 instead of a bunch of junk from the lack of a null-terminator is odd.

Any thoughts?

Thanks.

🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
We just have to assign the NULL value. Strictly speaking, NULL expands to an implementation-defined null pointer constant which is defined in many header files such as “stdio.h”, “stddef.h”, “stdlib.h” etc.
Published   January 10, 2025
🌐
SourceForge
sourceforge.net › home › browse › cppcheck › discussion
cppcheck / Discussion / General Discussion: False positive NULL pointer dereferece inside sizeof()
September 16, 2022 - sizeof(*foo) doesn't actually dereference foo, and so shouldn't generate a NULL pointer dereference warning. However, cppcheck will do so under at least some circumstances. For example: #include <stdio.h> struct foo { int a; char b; }; static void func(struct foo *foo) { size_t len = sizeof(*foo); ...
🌐
Guru99
guru99.com › home › c programming › difference between strlen() and sizeof() for string in c
Difference between strlen() and sizeof() for string in C
August 8, 2024 - It counts total characters which are presented in a string, eliminating the null character. The total number of characters in string includes, alphabets, special characters, and numbers, with blank spaces.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › difference-strlen-sizeof-string-c-reviewed
Difference between strlen() and sizeof() for string in C - GeeksforGeeks
July 23, 2025 - Type: Sizeof operator is a unary operator whereas strlen() is a predefined function in C · Data types supported: Sizeof gives actual size of any type of data (allocated) in bytes (including the null values) whereas get the length of an array ...