0

I have a problem with pointers that I don't know how to resolve. My pointer p points to a certain adress in memory. I want to point pointer q to the exact same adress. After that, I want to point pointer p to NULL. The problem is that after I do this, pointer q points to NULL as well, but I want him to keep pointing to the initial adress. Is there a way to do this? Here is a simple code of what I want to do:

    int* p = &a; //pointer "p" now points to "a"
    int* q;
    q = p; //pointer "q" points to "a" as well
    *p = NULL; //pointer "p" points to NULL
    printf("%d", *q); //pointer "q" points to NULL as well, but I want him to keep pointing to "a"
TommySVK
  • 13
  • 1
  • 6
  • 6
    `*p = NULL;` -> `p = NULL;` – Adrian Mole Apr 06 '20 at 09:28
  • 2
    I guess 5 answers focusing exactly the same should be enough. :-) – RobertS supports Monica Cellio Apr 06 '20 at 09:50
  • 1
    Do not let syntax like `int *p = NULL;` and `*p = 3;` confuse you. In a declaration, it might look like the `*` is part of the thing being given a value, but it is not. A declaration is special, and only the named object, `p`, is given a value, not `*p`. In an assignment, the entire expression on the left side, `*p` in `*p = 3`, is given a value. Thus, to set `p` to `NULL`, you want `p = NULL;`, not `*p = NULL;`. – Eric Postpischil Apr 06 '20 at 10:02

5 Answers5

1

In your code, p and q points to same memory location. You didn't change the pointer to different location, but overwrite the value pointed by p(which is also pointed by q) to NULL.

This should work.

    int a = 5;
    int* p = &a; //pointer "p" now points to "a"
    int* q = &a;
    p = q;
    p = NULL; //pointer "p" points to NULL
    printf("%d", *q);
Nandu Raj
  • 2,072
  • 9
  • 20
0

*p = NULL; //pointer "p" points to NULL that's wrong, you dereference p and set to NULL value it references to.

Use p = NULL.

fas
  • 1,393
  • 10
  • 20
0

The problem is here: *p = NULL; //pointer "p" points to NULL
You are not resetting where the pointer is pointing to, you are setting the value at the referenced location.

Just an example:

*ptr = value // (writes the value to the memory location of ptr)
ptr = value // (changes the referenced memory location to value)
CiaPan
  • 9,381
  • 2
  • 21
  • 35
0

The problem in your code is:

*p = NULL;

By that, you dereference p and write the value of NULL to a, which is wrong in your case. NULL is meant to get assigned or compared to pointers, not to the object a pointer points to (unless the referenced object, here a, would be also a pointer - (pointer to pointer)).

Solution:

You need to omit the dereference operator (*):

p = NULL;

to assign NULL to p and not to a.


Corrected code (Online Example):

#include <stdio.h>
int main (void)  
{
    int a = 5;
    int* p = &a; //pointer "p" now points to "a"
    int* q;

    q = p;       //pointer "q" points to "a" as well
    p = NULL;    //pointer "p" points to NULL

    printf("*q = %d\n", *q);
    printf("p = %p", (void*)p);
}

Output:

*q = 5
p = (nil)
Community
  • 1
  • 1
0

*p = NULL; //pointer "p" points to NULL

No it does not. You dereference this pointer and set the a to the NULL converted to int (which is usually zero)

If you want to set p to NULL you need to p = NULL;

If your idea was to have p referencing the q pointer you need to change the code to:

int a = 5;
int* q = &a;
int **p; 
p = &q;
*p = NULL; //now *p && q are NULL
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Re “No it does not”: Yes it does. `*p = NULL` puts `NULL` (converted to an `int`) in `*p`, and `p` points to `*p`, so `p` is then pointing to `NULL` (converted to an `int`). People should not use the phrase “pointing to NULL”. Rather, it should be “`p` is a null pointer” or “`p` points to nowhere/nothing”. – Eric Postpischil Apr 06 '20 at 10:00
  • @EricPostpischil no it does not. It points to the integer value. This value can be converted to NULL but it is not NULL any more. `Considering #define NULL (void *)0` . NULL is not `0` and `0` is not NULL – 0___________ Apr 06 '20 at 11:27
  • In order to teach, you need to understand what is in the minds of other people. It is not enough to write technically true statements and expect students to figure them out when they do not yet have the knowledge needed to figure them out. Some students will reason that “I put `NULL`in `*p`, so now `p` points to `NULL`.” The fact that `NULL` is converted to `int` during this assignment (which is why I wrote “`NULL` (converted to `int`)”) may not stop them from thinking this. So you have to explain; you have to deal with what is in their minds, not just what is in the C standard. – Eric Postpischil Apr 06 '20 at 11:39
  • Just writing answers that make technically true statements may show off your knowledge, but they do not necessarily convey that knowledge to students as well as answers that consider how students are thinking and that explain based on that. Answers should not merely just present a destination; they should lead the questioner from where they are to the destination. – Eric Postpischil Apr 06 '20 at 11:40
  • @EricPostpischil writing technically incorrect answers or comments makes much more confusion. But as I know it is very unlikely here to admit own mistake. – 0___________ Apr 06 '20 at 12:15
  • I am not saying to write incorrect answers. I am saying to think about what a student is thinking that is wrong and then not only state the correct answer but also explain to them what is incorrect in their thinking and help them fix it. – Eric Postpischil Apr 06 '20 at 12:19