Thread: Pointer question

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    Pointer question

    Code:
    #include<stdio.h>
    
    
    int main (void)
    {
    	int number = 10;
    	int *pointer;
        
    	pointer = &number;
    	
        printf("The value of var is: %d \n", number);
     
        printf("The address of var : %p \n", &pointer);
    	
    	pointer++;
    	
    	
    	printf("The address of var : %p \n", &pointer);
    
    
     
        return 0;
    }
    The value of var is: 10
    The address of var : 6422312
    The address of var : 6422312

    so address of variable is 6422312 and int is 4 bytes
    if i increment pointer it should be 6422316
    Why it show the same address after incrementing pointer ?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First off do you realize that you're not printing the address of *pointer but you're trying to print the address of **pointer?

    Perhaps you should see about increasing your compiler warning levels and fixing any warnings, such as:

    ||=== Build: Debug in chomework (compiler: GCC 8-1) ===|
    main.c||In function ‘main’:|
    main.c|13|warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int **’ [-Wformat=]|
    main.c|18|warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int **’ [-Wformat=]|
    ||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

    Two things, first do you see that "but argument 2 has type ‘int **’" part of the warning. It is telling you that you are passing a double pointer, not a single pointer (caused by that misplaced ampersand). Second you are using an incorrect type for the format specifier, invoking undefined behaviour, you need to cast that variable to a void* when using the "%p" format specifier.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    First off do you realize that you're not printing the address of *pointer but you're trying to print the address of **pointer?

    Perhaps you should see about increasing your compiler warning levels and fixing any warnings, such as:
    @jimblumberg my compiler doesn't show any warnings

    Code:
    #include<stdio.h> 
     
    int main (void)
    {
        int number = 10;
        int *pointer;
         
        pointer = &number;
         
        printf("The value of var is: %d \n", number);
      
        printf("The address of var : %p \n", &pointer);
         
        pointer++;
         
         
        printf("The address of var : %p \n", pointer);
     
     
      
        return 0;
    }
    Below is the output of code

    C:\Users\Vajra\Desktop>gcc -o test test.c


    C:\Users\Vajra\Desktop>test
    The value of var is: 10
    The address of var : 0061FF28
    The address of var : 0061FF30

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try it with
    gcc -Wall -Wextra -o test test.c
    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.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Try it with
    gcc -Wall -Wextra -o test test.c
    It tried this and it doesn't give any warning

    back to the question

    The value of var is: 10
    The address of var : 0061FF28
    The address of var : 0061FF30

    if i increment pointer it should be 0061FF32 int data type is 4 bytes why it is become 0061FF30

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Code:
    #include<stdio.h> 
      
    int main (void)
    {
        int number = 10;
        int *pointer;
          
        pointer = &number;
    
        printf("The size of an int is %zu\n", sizeof(int));  /* added this line just to make sure int is 4 "bytes" */
          
        printf("The value of var is: %d \n", number);
       
        printf("The address of var : %p \n", pointer);  /* Fixed this line here */
          
        pointer++;
          
          
        printf("The address of var : %p \n", pointer);
      
      
       
        return 0;
    }
    Output:
    Code:
    The size of an int is 4
    The value of var is: 10 
    The address of var : 0xffba888c 
    The address of var : 0xffba8890
    I don't see the problem now: 0xffba888c + 4 = 0xffba8890

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Hodor View Post
    Thanks hodor
    Output:
    Code:
    The size of an int is 4
    The value of var is: 10 
    The address of var : 0xffba888c 
    The address of var : 0xffba8890
    I don't see the problem now: 0xffba888c + 4 = 0xffba8890
    understood

    The address of var : 0xffba888c = 4290414732 decimal
    The address of var : 0xffba8890 = 4290414736 decimal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to pointer inderction question !!!?!?!?!
    By n@bu in forum C Programming
    Replies: 6
    Last Post: 02-15-2017, 02:56 AM
  2. Question about freeing pointer to pointer
    By Alpo in forum C Programming
    Replies: 2
    Last Post: 05-07-2014, 09:27 PM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. pointer question.... pointer theory?
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 02:29 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM

Tags for this Thread