Thread: array pointer basic

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    110

    array pointer basic

    I believe value of pointer should be equal to address of array element


    Code:
    #include<stdio.h>
    
    int main (void)
    
    
    {
        int i;
        
        int *pointer;
        
        int array[5] = {10, 20, 30, 40, 50};
        pointer = &array;
        
        for (i = 0; i <5; i++)
        {
               printf("Memory Allocation %p : [%d]   = %d \n", &array[i], i, array[i] );
        }
        
        for (i = 0; i <5; i++)
        {
            printf("%p : %d \n", (pointer++), (*pointer++));
        }
        
        
        printf("size of array : %d \n", sizeof(array));
        
        return 0;
    }
    Memory Allocation 0061FF14 : [0] = 10
    Memory Allocation 0061FF18 : [1] = 20
    Memory Allocation 0061FF1C : [2] = 30
    Memory Allocation 0061FF20 : [3] = 40
    Memory Allocation 0061FF24 : [4] = 50

    0061FF18 : 10
    0061FF20 : 30
    0061FF28 : 50
    0061FF30 : 3
    0061FF38 : 2404352

    size of array : 20

    address of array should be same as the pointer value

    In my program I am not getting output as I want, result show I am doing mistake. What's wrong with code

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,478
    A couple of things.

    First line 12 is incorrect (look at line 16 for a hint).

    Second line 21 is invoking undefined behaviour, you're attempting to increment the same value twice without a sequence point. You should probably move the increment to a separate line after the printf().

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    110
    Quote Originally Posted by jimblumberg View Post
    A couple of things.

    First line 12 is incorrect (look at line 16 for a hint).

    Second line 21 is invoking undefined behaviour, you're attempting to increment the same value twice without a sequence point. You should probably move the increment to a separate line after the printf().
    line 12
    Code:
     pointer = &array[0];
    problem with this line
    Code:
    printf("%p : %d \n", (pointer++), (*pointer++));
    I tried with different approach

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Pointer Help
    By Neti in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2012, 02:02 PM
  2. Basic pointer question
    By james123 in forum C Programming
    Replies: 4
    Last Post: 12-29-2009, 09:53 AM
  3. Basic pointer q
    By Jasper in forum C Programming
    Replies: 1
    Last Post: 11-28-2009, 02:44 AM
  4. basic of array and pointer
    By jeanette in forum C Programming
    Replies: 3
    Last Post: 05-14-2009, 10:17 AM
  5. Basic pointer question
    By MacNilly in forum C Programming
    Replies: 5
    Last Post: 06-19-2007, 08:31 AM

Tags for this Thread