lynks
lynks

Reputation: 5689

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.

Upvotes: 20

Views: 76952

Answers (6)

user2054656
user2054656

Reputation: 151

Your problem is, that my_pointer gets does not write to the pointer ptr, but its copy, *a.

You need to pass the address of ptr do to what you want.

Upvotes: 0

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;
}

Upvotes: 8

richselian
richselian

Reputation: 731

in C, a function call like foo(a) will never change the value of a.

Upvotes: 3

vszurma
vszurma

Reputation: 273

when passing the pointer to the function the pointer is copied into functions scope. you need to use a pointer of pointer if you want do such things. A pointer is basicly only an integer/long

Upvotes: 0

Tony The Lion
Tony The Lion

Reputation: 63190

Your function should take a char** a if you want to modify what it points to. This is because pointers are copied as arguments to a function, meaning that any changes you make to it inside, will not be seen outside the function, as its modifying a copy.

If you want to change it and see it outside the function scope, you need to add another indirection.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409166

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);

Upvotes: 47

Related Questions