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 ...
🌐
Bytes
bytes.com › home › forum › topic
Size of null - C / C++ - Bytes
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.
🌐
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?
June 29, 2015 -

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?

Find elsewhere
🌐
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 ...
🌐
Aticleworld
aticleworld.com › home › blog post › 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
🌐
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
🌐
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 ...
🌐
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.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 175717-read-string-length-when-string-contains-null-character.html
Read string length when string contains the null character
Since strlen(str) returns 3 because of the null character. ... #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int i; char str[] = {'h', 'e', 'j', '\0', 'a', 'b', 'c'}; int array_length = sizeof(str)/sizeof(str[0]); int characters_length = 0; for(i=0; i<array_length; i++) { if(str[i] == '\0') { str[i] = ' '; } printf("%c",str[i]); //The string character by character } /** alternative way to do it */ printf("\nLength of array: %d\n\n\n", sizeof(str)/sizeof(str[0])); printf("%s",str); //The whole string characters_length = strlen(str); //assign string length to a variable printf("\nThe string length is: %d\n\n",characters_length); //print the length return 0; }
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++ › manual › support.html
Chapter 4. Support
The G++ __null extension is defined so that sizeof(__null) == sizeof(void*) to avoid this problem. Scott Meyers explains this in more detail in his book Effective Modern C++ and as a guideline to solve this problem recommends to not overload on pointer-vs-integer types to begin with.
🌐
Quora
quora.com › What-is-the-size-of-a-null-string
What is the size of a null string? - Quora
In this case, the “string part” ... *) happens to be for the compiler you’re using, which is usually something between 2 to 8 bytes....