Home > front end >  Pointer Reference and Deference in C, Overriding value or Address?
Pointer Reference and Deference in C, Overriding value or Address?

Time:06-18

#include<stdio.h>

int main()

    int a=300;
    int *p=&a;
    int **q=&p;
    **q=-5000;
    *p=-200;strong text
    printf("%d",a); //&a
    printf("%d",p); //&p, *p
    printf("%d",q); //&q, *q, **q
    return 0;

}

Here the referencing to a variable is overriding value or address? If it is then why it not been changeable only ones? What is the point of using multiple pointers like one pointer is pointing to a value int, and another pointer q is pointing to pointer p? Is this necessary for one pointer can be accessed only by another pointer only? If it is then why?

CodePudding user response:

#include <stdio.h>

int main(void) {
    
    int a = 10, b = 20, c = 30;
    
    /*
        - These pointers all can point
          to an Integer value.
     
        - Unlike Integer variable a, b,
          and c above,Pointers do not
          store values such as Integers,
          Floats, Strings, etc.
     
        - Instead, a Pointer stores the
          Memory Location to where an
          Integer, Float, or String value
          is stored.
    */
    
    int *p, *q, *r;
    
    p = &a;
    
    q = p;
    
    r = &b;
    
    /*
        (&p) - Dereference a pointer:
     
                - Gives the Address (Location in Memory) of the pointer.
     
        (*p) - Reference a pointer:
     
                - Gives the Value located at that Address (Location in Memory).
    */
    
    printf("p: %d | %p | sizeof(p): %lu \n", *p, &p, sizeof(p));
    
    printf("q: %d | %p | sizeof(q): %lu \n\n", *q, &q, sizeof(q));
    
    
    printf("r: %d | %p | sizeof(r): %lu \n", *r, &r, sizeof(r));
    
    // *p and *r now have the same value, but are located at different Addresses in Memory.
    r = &a;
    
    printf("r: %d | %p | sizeof(r): %lu \n", *r, &r, sizeof(r));
    
    r = &c;
    
    printf("r: %d | %p | sizeof(r): %lu \n\n", *r, &r, sizeof(r));
    
/*
 
    - Pointers *p, *q, and *r all have differenct Addresses (Loactions in Memory).
 
       Value    Memory Location
    ____________________________
 *p |   10   |   0x16fdff230   |
    ----------------------------
 
       Value    Memory Location
    ____________________________
 *q |   10   |   0x16fdff228   |
    ----------------------------
 
 
    - Notice that *r has three different values, but the addresses
      (Locations in Memory) are all the same.
 
       Value    Memory Location
    ____________________________
 *r |   20   |   0x16fdff218   |
    ----------------------------
 
       Value    Memory Location
    ____________________________
 *r |   10   |   0x16fdff218   |
    ----------------------------
 
       Value    Memory Location
    ____________________________
 *r |   30   |   0x16fdff218   |
    ----------------------------
 
 */
    
    return 0;
}

Follow the link below for practice and more detailed information:

https://www.youtube.com/playlist?list=PL-fDNXex3rAb850huKS5WP6e8JsCJjYlW

CodePudding user response:

Here, **q and *p are changing the value stored in them, not the addresses.

I think the person who has created the program, has only created it for understanding pointers, not for spreading message that you can only access pointers using pointers. Actually you don't access pointers by pointers, they can be accessed like any other variable, but must not be done like that (unless you know that the memory address you're accessing is accessible by your program) because the addresses are given by the OS, and writing to an address not provided to your program will cause undefined behavour.

To understand pointers, you can read this porgram (the explanation is given at the end)-

#include <stdio.h>

int main() {
    int a = 300; // define variable 'a' with value 300
    int *p = &a; // define pointer 'p' and assign it the address of 'a'
    int b = *p; /* define variable 'b'. Note that now 'p' contains some address,
    which is the address of variable 'a'. Since now you're 'dereferencing' the
    value of 'p', which is an address, you'll have the value of 'b' to be equal
    to value of 'a'*/
    int **pp = &p; /* here we give the address of pointer 'p' to pointer
    to pointer 'pp'*/

    printf("a: %d\n", a); // print value of 'a'
    printf("*p: %d\n", *p); // print value of '*p' (which is equal to value of 'a')
    printf("p: %p\n", p); // this will print value of 'p' (which is address of 'a')
    printf("**pp: %d\n", **p); // this will print value of '**pp' (equal to 'a')
    printf("b: %d\n", b); // print value of 'b'

    return 0;
}

Pointers and pointers to pointers can be thought about like this (they in reality are like this)-

Variable a has value 300, and has an address in the memory, which is assigned by the OS.

Pointer p is defined equal to the address of variable a. It also has its own address in memory.

Variable b is defined equal to the value of the memory at a location. The location is given by value of p (which is address of variable a). It has its own address in memory.

Pointer to pointer pp is defined to be equal to address of pointer p. It also has its own address in the memory.

  • Related