0

Actually I was reading about pointers and wanted to try something ,so I wrote a small code

int main(){
    int x = 10;
   int *ptr;
    ptr = &x;                   
    printf("%d is stored at address %d\n",x,ptr );
    int *c=*ptr;
    c=&ptr;
   printf("location is %d\n",c);
 printf("value of c= %d",*c);
}

the result I expected was value of c would be 10 but instead the value came as the location of x. output: 10 is stored at address 997523644 location is 997523648 value of c= 997523644

does this problem arise because I didnt pass any location as first?or is it something else or my question is quite silly I know:D,Can anyone help me?

  • `int *c = *ptr` is equivalent to `int *c = 10`, but the initialization is irrelevant since you overwrite the value of `c` in the next line. `int **d = &ptr` would be a valid initialization of a pointer to pointer to int, but `c = &ptr` is attempting to store an `int **` in an `int *`. – William Pursell Jul 15 '21 at 03:46

4 Answers4

1

The first thing that is missing in the code that you have provided, is the declaration of ptr. You haven't declared the ptr variable yet, so first you need to do

int *ptr;

, which tells that you need a pointer variable to point to a int variable.

Since you declare another pointer int *c and ptr is already a pointer, you only need to assign int *c = ptr (or c=ptr in case you want to declare int *c seperately first), which will store the value of ptr (location of x) to c. So, doing int *c=*ptr is wrong. Now you can access the value of x by doing derefencing (*c or *ptr), which you already did.

Doing c=&ptr is also wrong, because c is of type int * and &ptr is of type int **. In case you are trying to store the address of ptr, you have to first declare a variable of type int **, for example int **d. Now d=&ptr is valid and d holds a value which is the address of ptr. Dereferencing d (*d) should give you the value that the address of ptr is holding, which is nothing but the address of x (same as the value of c). If you dereference d twice (**d), you should get the back the value of x (10).

XCSM
  • 99
  • 6
0

int* c = ptr should be enough, you can read it as: c is pointer to integer, c = ptr since ptr is already a pointer this will make both c and ptr point to the same value.

What you are currently doing: c=&ptr; is making c point to the address of ptr, so if you want to access x value you will need to dereference twice, once to get the address of ptr, and once again to get the content of x.

T.Aoukar
  • 653
  • 5
  • 19
0

Changed it to this:

#include <stdio.h>

int main(void) {
    int x = 10;
    int *ptr = &x;                   
    printf("%d is stored at address %p\n",x,ptr );
    int *c=ptr;
    int d = &ptr;
    printf("location is %p\n", c);
    printf("value of c= %d", *c);
    return 0;
}

As ptr is an address already, you can just do it:

int *c=ptr;

Output is:

10 is stored at address 0x7ffd6127dd54
location is 0x7ffd6127dd54
value of c= 10

To print variable address, you can check this answer: Stackoverflow Answer

Mukit09
  • 2,956
  • 3
  • 24
  • 44
0

You just need to remove the line

c=&ptr;

The above line means that you're setting C as the location of ptr, which is some random value. So C points to the location of ptr, and ptr points to the location of x. C is a double-pointer

Alternatively, if you keep that line in there, you can make it work by instead printing

printf("value of c= %d",**c);

either solution would get the output you expect