Thread: Need Help Understand Pointers in C

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    1

    Need Help Understand Pointers in C

    Hello, New Member here !! I have just started Learning C after a long time and Started Pointers in C. I have some doubts in P+1 (Pointers arithmetic's) below i have given a example code
    Code:
    #include <stdio.h>
    
    int main () 
    {
        int  a = 10;   /* normal variable declaration */
        int  *p;        /* pointer variable declaration */
        p = &a;  /* store address of a in pointer variable*/
        printf("Value of a variable: %d\n", a  );
        printf("Address of &a variable: %d\n", &a  );
        printf("\n");
    
        printf("Address stored in p : %d\n",p );
        printf("Value of *p: %d\n", *p );     
        printf("Address of &p variable: %d\n ", &p );
        printf("\n");
    
        printf("Address stored in p+1 variable: %d\n ", p+1);
        printf("Address of &p+1 variable: %d\n ", &p+1 );
        printf("value of *(p+1) variable: %d\n", *(p+1));
        printf("value of *p+1 variable: %d\n", *p+1);
        printf("\n");
    
        printf("Address stored in p+2 variable: %d\n ", p+2);
        printf("Address of &p+2 variable: %d\n ", &p+2);
        printf("value of *(p+2) variable: %d\n", *(p+2));
        printf("value of *p+2 variable: %d\n", *p+2);
         return 0;
    }
    OUTPUT :-
    Code:
    ~/Workspace/C$ ./ pointers 
    Value of a variable: 10
    Address of &a variable: 1116119516
    
    Address stored in p : 1116119516
    Value of *p: 10
    Address of &p : 1116119520
     
    Address stored in p+1 variable: 1116119520
    Address of &p+1 variable: 1116119528
    value of *(p+1) variable: 1116119516
    value of *p+1 variable: 11
    
    Address stored in p+2 variable: 1116119524
    Address of &p+2 variable: 1116119536
    value of *(p+2) variable: 32765
    value of *p+2 variable: 12
    Attachment 16315

    my doubt is, why Address of p (&p) and Address stored in p+1 is same. i know that by P+1 means is moving 4 bytes from the given address for integer which is p = 1116119516 + 4 --> P+1 = 1116119520, But &p is in 1116119520 before declaring P+1 which also gets 1116119520 as address. if both has same address wont there be some conflict in memory between those two if &p is called again.

    doubt 2, Why is Address stored in p: 1116119516 and value of *(p+1) variable: 1116119516 is same. is't *(p+1) suppose to get some garbage value because its doesn't have a variable declared.

    i don't understand this,
    if made some mistake in explaining things forgive me and help clear my stupid doubt
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Feb 2021
    Posts
    6
    why Address of p (&p) and Address stored in p+1 is same.
    You may be confusing the value and address of a and p.

    Code:
       Value            Address
    a  10               1116119516
    
    p  1116119516       1116119520
    p+1 is the value of p plus 4 bytes. *(p+1) is the value of p, because you are dereferencing 1116119520 which is 1116119516.

    The results can look weird at first glance because a and p are next to each other on the stack. That is an important thing to notice.
    Last edited by CoiledAlizarine; 02-25-2021 at 11:58 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,708
    You're confused, because it's a crap example to work from to begin with.

    The program is off in the weeds somewhere as soon as it starts doing anything with p+1

    Draw out some memory diagrams like this.
    Code:
          +----------+
    1000  |    10    | ;; a = 10
          +----------+
    1004  |   1000   | ;; p = &a
          +----------+
    But the compiler could do this instead
    Code:
          +----------+
    1000  |   1004   | ;; p = &a
          +----------+
    1004  |    10    | ;; a = 10
          +----------+
    Or even this
    Code:
          +----------+
    1000  |   1008   | ;; p = &a
          +----------+
    1004  |          | ;; invisible padding
          +----------+
    1008  |    10    | ;; a = 10
          +----------+
    The point is, it's a crap-shoot as to what you'll see in any given implementation when you start accessing pointers out of bounds.

    3 possible memory layouts, and the code is giving you 3 completely different answers, each with it's own unique 'voodoo' explanation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,395
    If your still struggling to understand it try thinking of each variable (that is a and p) as a house, houses come in many sizes but always have an address and contents, the contents of house p is the address of house a, the &a/&p is saying give me the address of the house I'm pointing at, the *p is saying give follow the address contained in house p to house a and get the contents.

    I don't think I missed anything there, gotta start work in a mo so I'll leave the rest to the other forum members

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    881
    I would change only:
    ...the &a/&p is saying give me the address of the house I'm pointing at.
    To
    ...the &a/&p is saying: give me the address of the house.
    Is a is house, &a is the address of the house. Same to "house" p.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,395
    Quote Originally Posted by flp1969 View Post
    I would change only:

    To

    Is a is house, &a is the address of the house. Same to "house" p.
    I said pointing with the image of the coder standing there literary pointing with their finger at the house, the & symbol being their finger

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having difficulty to understand pointers
    By Parth12 in forum C Programming
    Replies: 1
    Last Post: 02-18-2020, 06:33 AM
  2. Understand pointers
    By Aphex in forum C Programming
    Replies: 3
    Last Post: 08-12-2010, 09:07 PM
  3. i cant understand something in pointers
    By nik2 in forum C Programming
    Replies: 2
    Last Post: 02-12-2010, 01:26 PM
  4. Trying To Understand Pointers!
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 05-08-2008, 01:28 AM
  5. Something I still don't understand about pointers
    By Extol in forum C++ Programming
    Replies: 11
    Last Post: 03-01-2003, 06:20 AM

Tags for this Thread