Pointer Arithmetic (Basic stuff)

I was going through some old stuff these days and came across Pointer Arithmetic. I knew how it works ( or may be I should say I understand it but I was not exactly sure how it works). I decided to read it and started typing some examples. Really enjoyed it I thought I will share code. It has some comments. Feel free to ask if I am missing anything. It is pretty late night so I am not sure if I have written best code or comments. But I hope it will be fun to read it :)

#include <stdio.h>

//helper functions
void nosize(int array[]);  
void arrayOfPointers();  
void twoDimensionalArray();  
void pointerSubtraction();

int main()  
{
    int array[10];

    // %p to print pointer
    // & gives you address of array
     printf("This is first pointer address %p \n", &array[0]);

    /*
        Below statements are same as above. So basically an array
        declaration here is just a pointer to first location of
        data in the memory
    */
     printf("This is same as first pointer address %p \n", array);

     printf("This is same as first pointer address %p \n", &array);


    /*
        This is pointer Arithmetic. This gives you starting address
        of next element. In this case +4 because int is 4 byte of size
    */
     printf("This is a second pointer address %p \n", &array[0] + 1);



     printf("This is size of int %lu \n", sizeof(int));

     printf("This is size of address %lu \n", sizeof(&array));

     printf("This is size of pointer %lu \n", sizeof(array));


    nosize(array);
    arrayOfPointers();
    twoDimensionalArray();
    pointerSubtraction();
}


 // This int array[] will get only first pointer to array
 // Compiler translates arr's type to int *
 void nosize(int array[]) {
     printf("Size of array %lu  \n", sizeof(array));
 }


 //Seems like size of pointer on 64 bit machine is 8 byte 
 // but size of int is still 4 byte.

 /*
  confirmed above statement by compiling it using a flag with gcc 

  gcc -m32 

  This flag will force compilation in 32 bit even on 64 bit machines.

  In this case size of pointer is 4 byte.

 */
 void arrayOfPointers() {

     // This is an array of pointers.
     int *array [10];

     int **ptr = array; //get first pointer

     int **ptr3 = ptr + 2; //get 3rd pointer

     printf("First pointer %p \n", &ptr);

     printf("Third pointer %p \n", &ptr3);

     //get second pointer
     printf("second pointer %p \n", &ptr + 1);

     printf("size of array of pointers %lu \n", sizeof(array)); 
 }

 //A two-dimensional array is not the same as an array of pointers to 1D arrays
 void twoDimensionalArray() {
     int twoDimArray[10][12];

     //pointer to array of 10 elements. This is actual type of
     // 2 dimensional array. So each element in 2 dimensional array
     //repersents a pointer with 12 elements in above two
     // dimensional array 
     int (*array)[10];

     printf("array of  pointers array %lu \n", sizeof(array));

     printf("2 dimensional pointer array %lu \n", sizeof(twoDimArray));

 }


 void pointerSubtraction(){
       int array[10];

       int *p1 = array;

       int *p3 = array + 3; //or array[3]

       int diff = p3 - p1; //distance between two pointers.

       printf("Pointer substraction %d \n", diff);
 }

Outputs:

This is first pointer address 0xbff74b48  
This is same as first pointer address 0xbff74b48  
This is same as first pointer address 0xbff74b48  
This is a second pointer address 0xbff74b4c  
This is size of int 4  
This is size of address 4  
This is size of pointer 40  
Size of array 4  
First pointer 0xbff74ad0  
Third pointer 0xbff74acc  
second pointer 0xbff74ad4  
size of array of pointers 40  
array of  pointers array 4  
2 dimensional pointer array 480  
Pointer substraction 3  
Share on : Twitter, Facebook or Google+