It is undefined behaviour to de-reference a null pointer. The fix is simply to ensure that the pointer refers to an appropriate function.

In your case you want something like this:

void MyFunction(void)
{
    printf( "0\n" ); 
}

and then later you can assign to func0:

func0 = &MyFunction;

Note that I am using a different name for the function pointer variable and the actual function.

And now you can call the function, via the function pointer:

func0();
Answer from David Heffernan on Stack Overflow
Top answer
1 of 2
8

It is undefined behaviour to de-reference a null pointer. The fix is simply to ensure that the pointer refers to an appropriate function.

In your case you want something like this:

void MyFunction(void)
{
    printf( "0\n" ); 
}

and then later you can assign to func0:

func0 = &MyFunction;

Note that I am using a different name for the function pointer variable and the actual function.

And now you can call the function, via the function pointer:

func0();
2 of 2
6

I think you are mixing up naming and defining function pointers. I'll just point out that if you write

void func0(void) { printf( "0\n" ); }
void (*func0)(void);

you actually have two completely unrelated objects with the same name func0. The first func0 is a function, the second func0 is a variable with type pointer-to-function.

Assuming you declared your variable func0 globally (outside of any function), it will be automatically zero initialized, so the compiler will read your line

void (*func0)(void);

as

void (*func0)(void) = NULL;

So the variable func0 will be initialized with the value NULL, and on most systems NULL will actually be 0.

Your debugger is now telling you that your variable func0 has the value 0x0000, which is 0. So this is really no big surprise.

To your question regarding a "fix" - well, I assume you want a function pointer, pointing to your function func0, so you can do the following:

void func0(void) { printf( "0\n" ); }
void (*pFunc)(void) = func0;

or even better (although on most compilers not necessary) you can write

void (*pFunc)(void) = &func0;

so you initialize your variable pFunc (I highly recommend renaming it!) to point to func0. A bit more precise: You take the adress &... of the function func0 and assign this value to your variable pFunc.

Now you can "call" the function pointer (which means to call the function which the function pointer points to) by:

pFunc(); //will call function func0
Top answer
1 of 3
15

In C and C++, this is called undefined behaviour, meaning that this can lead to a Segmentation fault, nothing or whatever such a case will cause based on your compiler, the operating system you're running this code on, the environment (etc...) means.

Initializing a pointer to a function, or a pointer in general to NULL helps some developers to make sure their pointer is uninitialized and not equal to a random value, thereby preventing them of dereferencing it by accident.

2 of 3
2
  1. What happnes when u try to access NULL? Following is true about data as well as code, and this is what happens when you try to read NULL(or any address from 0 to 4096,i.e atleast first page of segment). Root cause of this lies in OS and microprocessor segmentation/paging architecture

    When you try to access NULL( or 0) address, in any of data or code section, it causes segmentation fault(which is actually a killer page fault). First page of section is treated as out of( or invalid part of) virtual address space. That is purposefully that first page is kept invalid( or not present) so atleast one address that pointer contains could be represented as invalid in program at execution time.

    Page descriptor of the 1st page(which contains virtual address 0, NULL), has first bit "present" as 0 (means its invalid page). Now if you try to access NULL pointer(0 address) it will cause to raise a page fault as page is not present, and OS will try to handle this page fault. When page fault handler see that its trying to access 1st page, which is treated as a invalid part of virtual address space it kills the process. This is all about user space process. If you try to access NULL pointer in system process(kernel level code), it will fail your OS an crash the system.

    Links: http://en.wikipedia.org/wiki/Page_fault#Invalid http://en.wikipedia.org/wiki/Memory_protection#Paged_virtual_memory http://pdos.csail.mit.edu/6.828/2005/readings/i386/s05_02.htm

    Above is sufficient bt as i think u should read this as well http://www.iecc.com/linker/linker04.txt

  2. Why function pointer is initialized to NULL? Although if you try to call the with NULL its going to give page/segment fault. NULL signifies its invalid function. If it contains any garbage address but in valid virtual address space of code section, i think any code at that address will be called, which could be even more disaster(spl in case of real time systems). Initialize funcp = funct_foo_name + 1; now call function using function pointer. Function pointer points to valid virtual address space of code section. bt function will start from incorrect place to execute. which could result into wrong code execution or wrong order.

Discussions

is there a way to pass NULL to an function in c that requires arguments in c?
C has no optional arguments and there is no direct way to test if an argument has been supplied or not. If you need this sort of functionality, add a sentinel value to the possible values for the argument that can be passed to indicate the absence of a value. Alternatively, add an additional argument that says if the argument is valid. Alternatively, provide two functions, one with and one without the argument. The correct solution depends a lot on your specific use case. More on reddit.com
🌐 r/C_Programming
14
0
June 30, 2022
What does it mean to do a "null check" in C or C++? - Software Engineering Stack Exchange
Without the null check, passing a NULL pointer into this function will cause a segfault, and there is nothing you can do - the OS will simply kill your process and maybe core-dump or pop up a crash report dialog. More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
Can I reliably set a function pointer to NULL in C and C++? - Stack Overflow
In P.J. Plauger's book, The Standard C Library, he warns about assigning a function pointer to NULL. Specifically, he says this: The macro NULL serves as an almost-universal null pointer constant... More on stackoverflow.com
🌐 stackoverflow.com
pointers - How do some C functions accept null parameters? - Stack Overflow
I always thought that C does not accept NULL parameters, until I started learning about pointers. In some programming languages, like python for one, it is possible to pass a NULL parameter as an More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › c › c_null.php
C NULL
It helps you avoid using pointers that are empty or invalid. You can compare a pointer to NULL to check if it is safe to use. Many C functions return NULL when something goes wrong. For example, fopen() returns NULL if a file cannot be opened, and malloc() returns NULL if memory allocation fails.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
We can pass a NULL value for pointers that should not contain any value to the function in C.
Published: January 10, 2025
Top answer
1 of 6
30

In C and C++, pointers are inherently unsafe, that is, when you dereference a pointer, it is your own responsibility to make sure it points somewhere valid; this is part of what "manual memory management" is about (as opposed to the automatic memory management schemes implemented in languages like Java, PHP, or the .NET runtime, which won't allow you to create invalid references without considerable effort).

A common solution that catches many errors is to set all pointers that don't point to anything as NULL (or, in correct C++, 0), and checking for that before accessing the pointer. Specifically, it is common practice to initialize all pointers to NULL (unless you already have something to point them at when you declare them), and set them to NULL when you delete or free() them (unless they go out of scope immediately after that). Example (in C, but also valid C++):

void fill_foo(int* foo) {
    *foo = 23; // this will crash and burn if foo is NULL
}

A better version:

void fill_foo(int* foo) {
    if (!foo) { // this is the NULL check
        printf("This is wrong\n");
        return;
    }
    *foo = 23;
}

Without the null check, passing a NULL pointer into this function will cause a segfault, and there is nothing you can do - the OS will simply kill your process and maybe core-dump or pop up a crash report dialog. With the null check in place, you can perform proper error handling and recover gracefully - correct the problem yourself, abort the current operation, write a log entry, notify the user, whatever is appropriate.

2 of 6
8

The other answers pretty much covered your exact question. A null check is made to be sure that the pointer you received actually points to a valid instance of a type (objects, primitives, etc).

I'm going to add my own piece of advice here, though. Avoid null checks. :) Null checks (and other forms of Defensive Programming) clutter code up, and actually make it more error prone than other error-handling techniques.

My favorite technique when it comes to object pointers is to use the Null Object pattern. That means returning a (pointer - or even better, reference to an) empty array or list instead of null, or returning an empty string ("") instead of null, or even the string "0" (or something equivalent to "nothing" in the context) where you expect it to be parsed to an integer.

As a bonus, here's a little something you might not have known about the null pointer, which was (first formally) implemented by C.A.R. Hoare for the Algol W language in 1965.

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

Top answer
1 of 2
15
 int (*pfun) (void) = NULL;  

It is actually valid.

The C rules of assignment says that:

(Note that's an initialization here but the same type of constraints and conversions as for simple assignment apply.)

(C99, 6.5.16.1 Simple assignment p1 Constraints) "One of the following shall hold: [...] — the left operand is a pointer and the right is a null pointer constant;"

and

(C99, 7.17p3) "The macros are NULL which expands to an implementation-defined null pointer constant;"

So assigning a null pointer constant to any pointer (object pointer, function pointer or void *) is allowed by C. Note that Plauger's book refers to C89 when he mentions Standard C but the wording of the assignment constraints are the same in C89.

2 of 2
11

void (*test)() = (void*)0 => produced an invalid conversion error in both gcc and g++

GCC detects the language based on the file extension. Compiling a .cc file with GCC will invoke the C++ compiler, not the C compiler.

Try it with a C source file and you'll see this is accepted. As it should be, as it's allowed by the standard.

If so, why does assigning to NULL work all the time, but assigning to (void*)0 not?

NULL isn' t allowed to be defined as ((void*)0) in C++ mode. In C++ mode, it must be defined as either an integral constant with value 0, or as nullptr. Either can be converted to any function pointer type.

Can I always reliably set a function pointer to NULL in C and C++?

Yes, in any conforming C or C++ implementation this will work.

Find elsewhere
Top answer
1 of 4
9

In some programming languages [...] it is possible to pass a NULL parameter as an argument, but in C I always thought this would result in Undefined Behavior.

Passing a NULL parameter for a pointer by itself does not result in UB; it's attempting to access the memory pointed to by a pointer set to NULL that does.

Passing NULL is a very common practice for situations when something is not specified. The caller is expected to check parameters for NULL before performing the access. For example, the standard lets you pass NULL to free, which makes the function a lot more convenient.

don't NULL pointers simply point to nothing?

Yes, they do. But that "nothing" is globally well-known, so using a NULL lets you communicate the fact that a pointer points to nothing to functions that you call. In other words, the check

if (myPointer == NULL)

is well-defined*, so you can use it to your advantage.

* Unless you use a dangling pointer, i.e. a pointer that you have freed, or a pointer that points to object that went out of scope. You can prevent the first situation from happening by assigning NULL to every pointer that you free(), and the second situation by declaring pointers in the scope that has the same or higher level of nesting as the scope of an automatic object to which the pointer is pointing.

2 of 4
4
void func_with_optional_arg(char *optional)
{
    if (optional == NULL) {
        // do something differently
    }

    /* ... */
}

Why would that invoke UB? Dereferencing a NULL pointer certainly would, but passing one around does not. NULL is a sentinel value used to determine whether or not a pointer is valid (not that invalid pointers cannot have other values, but we use this one explicitly.) If passing it to a function invoked UB then what would be the point of its existence in the first place?

whereas passing a NULL non-pointer value is not?

There is no such thing as a "NULL non-pointer" in C, so I'm not sure what you mean here.

🌐
Flavio Copes
flaviocopes.com › home › how to use null in c
How to use NULL in C - Flavio Copes
February 13, 2020 - Under the hood, NULL is typically defined as ((void *)0) or 0. Don’t confuse it with '\0', the null character that terminates C strings.
Top answer
1 of 2
7

Actually, you can use a literal 0 anyplace you would use NULL.

Section 6.3.2.3p3 of the C standard states:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

And section 7.19p3 states:

The macros are:

NULL

which expands to an implementation-defined null pointer constant

So 0 qualifies as a null pointer constant, as does (void *)0 and NULL. The use of NULL is preferred however as it makes it more evident to the reader that a null pointer is being used and not the integer value 0.

2 of 2
5

NULL is used to make it clear it is a pointer type.

Ideally, the C implementation would define NULL as ((void *) 0) or something equivalent, and programmers would always use NULL when they want a null pointer constant.

If this is done, then, when a programmer has, for example, an int *x and accidentally writes *x = NULL;, then the compiler can recognize that a mistake has been made, because the left side of = has type int, and the right side has type void *, and this is not a proper combination for assignment.

In contrast, if the programmer accidentally writes *x = 0; instead of x = 0;, then the compiler cannot recognize this mistake, because the left side has type int, and the right side has type int, and that is a valid combination.

Thus, when NULL is defined well and is used, mistakes are detected earlier.

In particular answer to your question “Is there a context in which just plain literal 0 would not work exactly the same?”:

  • In correct code, NULL and 0 may be used interchangeably as null pointer constants.
  • 0 will function as an integer (non-pointer) constant, but NULL might not, depending on how the C implementation defines it.
  • For the purpose of detecting errors, NULL and 0 do not work exactly the same; using NULL with a good definition serves to help detect some mistakes that using 0 does not.

The C standard allows 0 to be used for null pointer constants for historic reasons. However, this is not beneficial except for allowing previously written code to compile in compilers using current C standards. New code should avoid using 0 as a null pointer constant.

🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_macro_null.htm
C library - NULL Macro
Following is the basic C library macro NULL Macro to see its demonstration on file handling. #include <stddef.h> #include <stdio.h> int main () { FILE *fp; fp = fopen("file.txt", "r"); if( fp != NULL ) { printf("Opend file file.txt successfully\n"); fclose(fp); } fp = fopen("nofile.txt", "r"); if( fp == NULL ) { printf("Could not open file nofile.txt\n"); } return(0); }
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › c programming languages › how to check null in c: 7 steps (with pictures) - wikihow
How to Check Null in C: 7 Steps (with Pictures) - wikiHow
June 9, 2025 - An unassigned pointer still points to a memory address, just not one that you have specified. It's common practice to set newly created or newly freed pointers to NULL to make sure you don't use this unhelpful address by accident. Avoid this mistake: char *ptr; if(ptr == NULL) { //This will return FALSE.
Top answer
1 of 3
12

In C, NULL is a macro that expands either to 0 or (void*)0 (or something that has a similar effect).

In the first case, you can not differentiate between NULL and 0, because they are literally the same.
In the second case, your code will cause a compile error, because you can't compare an integer variable with a pointer.

2 of 3
4

First some background ...


The macros are NULL which expands to an implementation-defined null pointer constant; C11 §7.19 3

NULL typically is an integer constant 0 or (void*)0 or the like. It may have a different implementation or type - It could be ((int*) 0xDEADBEEF) as strange as that may be.

NULL might be type int. It might be type void * or something else. The type of NULL is not defined.


When the null pointer constant NULL is cast to any pointer, is is a null pointer. An integer 0 cast to a pointer is also a null pointer. A system could have many different (bit-wise) null pointers. They all compare equally to each other. They all compare unequally to any valid object/function. Recall this compare is done as pointers, not integers.

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function. C11 §6.3.2.3 3

int x;
if (&x == NULL) ... // this is false

So after all that chapter and verse how to distinguish NULL from 0?

If the macro NULL is defined as an int 0 - it is game over - there is no difference between 0 and NULL.

If NULL is not an int, then code can use _Generic() to differentiate NULL and 0. This does not help OP's "Any change made can only be made within the function itself." requirement as that function accepts an int augment.

If NULL is an int that has a different bit-pattern than 0, then a simple memcmp() can differentiate.

I suspect the whole reason for this exercise is to realize there is no portable method to distinguish NULL from 0.

🌐
ThoughtCo
thoughtco.com › definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - The C and C++ programming, a pointer is a variable that holds a memory location. The null pointer is a pointer that intentionally points to nothing. If you don't have an address to assign to a pointer, you can use null.
🌐
Go Forum
forum.golangbridge.org › getting help
Passing NULL to a C function - Getting Help - Go Forum
July 14, 2020 - I am new to GoLang and writing a C-GO binding. The C Function takes an argument which is a const char *. This works fine if I pass NULL for the argument in C. Now in my GO implementation I need to pass NULL in the C call. func AvformatMyfunc(ctx **Context, o *OutputFormat, fo, fi string) int { Cformat_name := C.CString(fo) defer C.free(unsafe.Pointer(Cformat_name)) Cfilename := C.CString(fi) defer C.free(unsafe.Pointer(Cfilename)) return int(C.myfunc((**C.struct_AVFormatContext)(unsafe...
🌐
GNU
gnu.org › software › libc › manual › html_node › Null-Pointer-Constant.html
Null Pointer Constant (The GNU C Library)
If you use the null pointer constant as a function argument, then for complete portability you should make sure that the function has a prototype declaration. Otherwise, if the target machine has two different pointer representations, the compiler won’t know which representation to use for that argument.
🌐
Scaler
scaler.com › home › topics › what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - Explanation: In the above example, we initialized three pointers as *ptr1, *ptr2, and *ptr3, and we assigned a value to the num variable in *ptr1 and compared it by 0 because *ptr1 is not equal to null, therefore it would output the result as NOT NULL, and we did not assign any value to *ptr2 and As a result, the output will be printed as NOT NULL. We assigned value 0 to *ptr3, which is equal to null, hence the output will be NULL. Assume you want func(int* i) to be called from the main function.
🌐
Quora
quora.com › How-do-you-return-a-null-pointer-in-C
How to return a null pointer in C - Quora
Answer (1 of 7): “How do you return a null pointer in C?” This will do it: [code]return (void*)0; [/code]Enjoy your null pointer!