Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Assign Pointer to NULL

I am misunderstanding something basic about pointers in C, this should be simple but search brings up nothing. I do not understand the behaviour of the following code;

#include <stdlib.h>
#include <stdio.h>

void my_function(char *);

int main(int argc, char *argv[]) {
    char *ptr;
    ptr = malloc(10);

    if(ptr != NULL) printf("FIRST TEST: ptr is not null\n");
    else printf("FIRST TEST: ptr is null\n");

    my_function(ptr);

    if(ptr != NULL) printf("SECOND TEST: ptr is not null\n");
    else printf("SECOND TEST: ptr is null\n");
}

void my_function(char *a) {
    a = NULL;
}

Which outputs;

FIRST TEST: ptr is not null
SECOND TEST: ptr is not null

Why does the second test still see the pointer as not NULL? I am trying to use a NULL pointer assignment as a sort of 'return flag' to indicate a certain failure of the function. But upon testing the pointer afterwards, it does not seem to be NULL.

like image 864
lynks Avatar asked Apr 30 '13 12:04

lynks


People also ask

Can you assign a pointer to null?

It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

Can you initialize a pointer to null in C?

A pointer can also be initialized to null using any integer constant expression that evaluates to 0, for example char *a=0; . Such a pointer is a null pointer.

How do you define a null pointer?

A null pointer is a pointer which points nothing. Some uses of the null pointer are: a) To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet. b) To pass a null pointer to a function argument when we don't want to pass any valid memory address.


2 Answers

It's because the pointer is passed by value and not by reference. If you want to change the pointer inside the function you need to pass the actual pointer as a pointer, i.e. a pointer to a pointer:

void my_function(char **a)
{
    *a = NULL;
}

Use the address-of operator & when you call the function to get the address of the pointer:

my_function(&ptr);
like image 65
Some programmer dude Avatar answered Sep 19 '22 08:09

Some programmer dude


Your statement a=NULL in my_function() indeed sets the value of a to NULL, but a is a local variable of that function.When you passed ptr to my_function() in main(), the value of ptr was copied to a.I suppose your whole confusion arose from the * used before a in the definition of my_function().

Pointers are generally passed to functions when we want to manipulate the original values which those pointers point to, from the called function, and this is done by dereferencing those pointers from the called functions.In this case, had you used this:

*a= blah blah;

it would have reflected in the value at the address pointed to by ptr in main().But since you want to change the value of ptr itself, you need to be able to have a way to manipulate it from my_function().For this you use a pointer-to-pointer,ie of type char**.You pass such a char** as argument to my_function(() and use it to alter the value of ptr.Here's the variation to your code that would do it for you:

#include <stdlib.h>
#include <stdio.h>

void my_function(char **); // Change char* to char**

int main(int argc, char *argv[]) {
    char *ptr;
    ptr = malloc(10);

    if(ptr != NULL) printf("FIRST TEST: ptr is not null\n");
    else printf("FIRST TEST: ptr is null\n");

    my_function(&ptr); //You pass a char**

    if(ptr != NULL) printf("SECOND TEST: ptr is not null\n");
    else printf("SECOND TEST: ptr is null\n");
}

void my_function(char **a) {  //Change char* to char** here
    *a = NULL;
}
like image 38
Rüppell's Vulture Avatar answered Sep 20 '22 08:09

Rüppell's Vulture