-1
#include <stdio.h>
int main()
{

    
    int *p = (int *)malloc(sizeof(int));
    *p = 45;
    int *v = p;
    *v = 41;
    printf("%d", *p);
    free(p);
    return 0;

}

The output of this code is 41. But I don't understand how the output came as 41 if *p was 45. Can someone please explain it??

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    [Please see this discussion on why not to cast the return value of malloc() and family in C..](https://stackoverflow.com/q/605845/2173917) – Sourav Ghosh Jul 29 '20 at 15:12
  • @SouravGhosh This is especially relevant in this specific question, as the OP is not including `stdlib.h`. I am surprised non of the answers mention it. – Eugene Sh. Jul 29 '20 at 15:25
  • @EugeneSh. Did not want to have it duplicated, as already left a comment. Anyways, will add to the answer itself. – Sourav Ghosh Jul 29 '20 at 15:31
  • I am new to programming and not at a very advance level. So I could not figure it out. – Sarvesh Vinayak Jul 29 '20 at 15:38

3 Answers3

1

First, Please see this discussion on why not to cast the return value of malloc() and family in C...

That said, note the line

 int *v = p;

here, v was made to point to the same memory address as that of the p. So, any change made to the value at address via pointer v will be reflected while accessing the memory via p and vice-versa.

To present it graphically, let's say , malloc() returned a pointer address 0x8000 which is stored in p, and we have the value 45 stored in the memory address pointed to by p (in the address 0x8000).

Now, by saying int *v = p;, we're making p also to point to 0x8000 memory address. Deferencering either v or p will use the content of the memory 0x8000.

            +--------------------------+
            |                          |
            |          45              |
            |                          |
            +--------------------------+
            0x8000


+----------------+        +--+-------------+
|  p = 0x8000    |        |  v = p = 0x8000|
|                |        |                |
+----------------+        +----------------+
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

With the statement

int *p = malloc(sizeof(int));

you have something like this:

+---+     +----------------------------+
| p | --> | memory allocated by malloc |
+---+     +----------------------------+

Then with the statement

int *v = p;

you make v point to the same location where p is pointing (you're copying the pointer not the memory it points to), which means you have something like this:

+---+
| p | --\
+---+    |    +----------------------------+
         >--> | memory allocated by malloc |
+---+    |    +----------------------------+
| v | --/
+---+

Hopefully it should be clear what happens when you dereference either p or v.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

p is a pointer the value of which is the address of the allocated memory where the value 45 is written

int *p = (int *)malloc(sizeof(int));

*p = 45;

v is also a pointer that is assigned with the value of p that is with the same address of the allocated memory

int *v = p;

So the next statement

*v = 41;

changes the value at the address that the same is stored in the both pointers. That is the both pointers point to the same object and you may use any of them to change the value of the pointed object.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335