Thread: Need Help Understand Pointers in C

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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