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
🌐
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 ...
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).

Discussions

Understanding difference between 0 and NULL
NULL is a macro that expands to a null pointer constant. 0 is a null pointer constant. That means NULL could just be defined as 0. Put simply, sizeof (NULL) is simply not something you can rely on as having a consistent, implementation-independent meaning — you don't even know what type NULL has. It is entirely possible for sizeof (NULL) to be not equal to sizeof (void *). If you want to know how big a particular pointer type is, use sizeof on that pointer type, or on a value of that pointer type. More on reddit.com
🌐 r/C_Programming
36
15
July 2, 2023
c - Sizeof() of pointer pointing to NULL - Stack Overflow
Each one of these expressions is ... during compilation. So the value of var during runtime has no effect on either one of these expressions. ... sizeof simply returns the size of *abcp and this pointer has size 8 in your machine. It doesn't matter if the address stored in the pointer is valid or not (NULL is usually ... More on stackoverflow.com
🌐 stackoverflow.com
August 28, 2014
c - sizeof for a null terminated const char* - Stack Overflow
Find centralized, trusted content ... Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... how do I make sure that string 'a' is null terminated? when a = "abcd" and I do sizeof(a), I get ... More on stackoverflow.com
🌐 stackoverflow.com
null terminated - Does '\0' Take Up The Size of a Char? - C - Stack Overflow
PART 2 - of Compare CCT (Correlated ... Siess in tkinter GUI application · Are Jesus' breathing on the disciples and coming of the Holy Spirit separate actions? ... Are U.S. federal officials required to identify themselves when entering a fenced area of a home to make an arrest? Why does is.na(NULL) returns ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 › 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
🌐
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!
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 to Python · 7.1M learners · Introduction to Java · 4.7M learners ...
🌐
Aticleworld
aticleworld.com › home › what is a null pointer in c/c++?
What is a Null Pointer in C/C++? - Aticleworld
January 25, 2023 - The null pointer is an integer constant expression with the value 0 or such an expression cast to type void pointer. Dereferencing of null pointer would cause undefined behavior. int* ptr = NULL; // normally points to zero or implementation-defined location. Yes, you can use the sizeof operator ...
🌐
C For Dummies
c-for-dummies.com › blog
The Difference Between NULL and Zero | C For Dummies Blog
Finally, to prove each item is of the appropriate type, the code outputs these lines: ... The int value \0 is 4 bytes long (on my computer), which helps confirm it’s an int. But the NULL value is 8 bytes long, which is the size of an address.
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Null_character
Null character - Wikipedia
March 3, 2026 - The common competing design for a string stores the length of the string as an integer data type, but this limits the size of the string to the range of the integer (for example, 255 for a byte).
🌐
Unstop
unstop.com › home › blog › null pointer in c | a detailed explanation with examples
Null Pointer In C | A Detailed Explanation With Examples
May 3, 2024 - We create an integer pointer called array and use it with the malloc() function to allocate memory for an array with number of elements given by the size variable. The size in bytes for each element is determined using the sizeof() operator. Then, the pointer variable array is assigned the address to the memory block allocated to the integer array. We then check if the pointer is NULL using an if-statement.
🌐
Quora
quora.com › What-is-the-purpose-of-using-NULL-as-an-array-size-in-C-C
What is the purpose of using NULL as an array size in C/C++? - Quora
In C, NULL is the constant 0, wrapped in a macro to use as a nil pointer value. You certainly can create arrays of size 0, but they are pointless. Or they are a placeholder for an array of undefined size at the end of a structure in C, but there ...
🌐
Quora
quora.com › What-is-the-size-of-a-null-string
What is the size of a null string? - Quora
* In a language like C, you can have a char pointer that is set to NULL. In this case, the “string part” isn’t represented, and the only space used is the space for the char pointer itself.
🌐
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).
Top answer
1 of 11
445

Note: This answer applies to the C language, not C++.


Null Pointers

The integer constant literal 0 has different meanings depending upon the context in which it's used. In all cases, it is still an integer constant with the value 0, it is just described in different ways.

If a pointer is being compared to the constant literal 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant. The C standard defines that 0 cast to the type void * is both a null pointer and a null pointer constant.

Additionally, to help readability, the macro NULL is provided in the header file stddef.h. Depending upon your compiler it might be possible to #undef NULL and redefine it to something wacky.

Therefore, here are some valid ways to check for a null pointer:

if (pointer == NULL)

NULL is defined to compare equal to a null pointer. It is implementation defined what the actual definition of NULL is, as long as it is a valid null pointer constant.

if (pointer == 0)

0 is another representation of the null pointer constant.

if (!pointer)

This if statement implicitly checks "is not 0", so we reverse that to mean "is 0".

The following are INVALID ways to check for a null pointer:

int mynull = 0;
<some code>
if (pointer == mynull)

To the compiler this is not a check for a null pointer, but an equality check on two variables. This might work if mynull never changes in the code and the compiler optimizations constant fold the 0 into the if statement, but this is not guaranteed and the compiler has to produce at least one diagnostic message (warning or error) according to the C Standard.

Note that the value of a null pointer in the C language does not matter on the underlying architecture. If the underlying architecture has a null pointer value defined as address 0xDEADBEEF, then it is up to the compiler to sort this mess out.

As such, even on this funny architecture, the following ways are still valid ways to check for a null pointer:

if (!pointer)
if (pointer == NULL)
if (pointer == 0)

The following are INVALID ways to check for a null pointer:

#define MYNULL (void *) 0xDEADBEEF
if (pointer == MYNULL)
if (pointer == 0xDEADBEEF)

as these are seen by a compiler as normal comparisons.

Null Characters

'\0' is defined to be a null character - that is a character with all bits set to zero. '\0' is (like all character literals) an integer constant, in this case with the value zero. So '\0' is completely equivalent to an unadorned 0 integer constant - the only difference is in the intent that it conveys to a human reader ("I'm using this as a null character.").

'\0' has nothing to do with pointers. However, you may see something similar to this code:

if (!*char_pointer)

checks if the char pointer is pointing at a null character.

if (*char_pointer)

checks if the char pointer is pointing at a non-null character.

Don't get these confused with null pointers. Just because the bit representation is the same, and this allows for some convenient cross over cases, they are not really the same thing.

References

See Question 5.3 of the comp.lang.c FAQ for more. See this pdf for the C standard. Check out sections 6.3.2.3 Pointers, paragraph 3.

2 of 11
45

It appears that a number of people misunderstand what the differences between NULL, '\0' and 0 are. So, to explain, and in attempt to avoid repeating things said earlier:

A constant expression of type int with the value 0, or an expression of this type, cast to type void * is a null pointer constant, which if converted to a pointer becomes a null pointer. It is guaranteed by the standard to compare unequal to any pointer to any object or function.

NULL is a macro, defined in as a null pointer constant.

\0 is a construction used to represent the null character, used to terminate a string.

A null character is a byte which has all its bits set to 0.

🌐
Northern Illinois University
faculty.cs.niu.edu › ~winans › CS501 › Notes › cstrings.html
C Strings
The length of a null string is 0. ... This declaration creates an unnamed character array just large enough to hold the string "Karen" (including room for the null character) and places the address of the first element of the array in the char pointer name:
Top answer
1 of 6
88

It doesn't.

The string terminator is a byte containing all 0 bits.

The unsigned int is two or four bytes (depending on your environment) each containing all 0 bits.

The two items are stored at different addresses. Your compiled code performs operations suitable for strings on the former location, and operations suitable for unsigned binary numbers on the latter. (Unless you have either a bug in your code, or some dangerously clever code!)

But all of these bytes look the same to the CPU. Data in memory (in most currently-common instruction set architectures) doesn't have any type associated with it. That's an abstraction that exists only in the source code and means something only to the compiler.

Edit-added: As an example: It is perfectly possible, even common, to perform arithmetic on the bytes that make up a string. If you have a string of 8-bit ASCII characters, you can convert the letters in the string between upper and lower case by adding or subtracting 32 (decimal). Or if you are translating to another character code you can use their values as indices into an array whose elements provide the equivalent bit coding in the other code.

To the CPU the chars are really extra-short integers. (eight bits each instead of 16, 32, or 64.) To us humans their values happen to be associated with readable characters, but the CPU has no idea of that. It also doesn't know anything about the "C" convention of "null byte ends a string", either (and as many have noted in other answers and comments, there are programming environments in which that convention isn't used at all).

To be sure, there are some instructions in x86/x64 that tend to be used a lot with strings - the REP prefix, for example - but you can just as well use them on an array of integers, if they achieve the desired result.

2 of 6
5

In short there is no difference (except that an int is 2 or 4 bytes wide and a char just 1).

The thing is that all modern libaries either use the null terminator technique or store the length of a string. And in both cases the program/computer knows it reached the end of a string when it either read a null character or it has read as many characters as the size tells it to.

Issues with this start when the null terminator is missing or the length is wrong as then the program starts reading from memory it isn't supposed to.