× Home
Next Lec → ← Previous Lec

Arrays and Pointers Arithmetic

Pointer Arithmetic

There are four arithmetic operators that can be used on pointers:

Pointers arithmetic is not same as normal arithmetic i.e. if you want to add 1 to any no. then you will get that no. after adding 1 in it. But in Pointers it is a bit different as: Base Address : The first byte address of any variable is known as base address. It means suppose you have int type variable then it's size in my architecture is 4 byte (It may vary in yours) then 4 consecutive blocks will be created in RAM. So when we will point it with any pointer variable at that time the address of 1 block of 4 will come in that pointer variable. It means base address is the first block address of any data type variable.

  • We can't add, multiply or divide two addresses. (Subtraction is possible)
                                    
    
                                        // E.g:
                                        int a, b, *p, *q;
                                        
                                        &a*&b; // Not Possible
                                        
                                        &a+&b; // Not Possible
                                        
                                        p*q; // Not Possible
                                    
                                
  • We can’t multiply & divide an address with integer value.
                                    
    
                                        // E.g :
    
                                        &a*5; // Not Possible
                                       
                                        p/5; // Not Possible
                                    
                                
  • We can add or subtract integer to/from an address.
                                    
                                    
                                  // E.g :
                                 
                                  int a,b; // Let's Suppose &a is 1000
                                 
                                  int *p, *q;
                                 
                                  p=&a;
                                 
                                  q=&b;
                                 
                                  p+1; // It is possible [1000+1 = 1004]
                                
                            

Basically when we do
int a = 2;
a++; → 3

But in pointer
int *ptra = &a;
ptra = ptra + 1;
Now here it will add size of ptra (size of int)

Doing Arithmetic

                        

                                #include <stdio.h>
                                int main()
                                {
                                int a = 34;
                                int *ptra = &a;
                                printf("%d", ptra); // lets say this is x
                                printf("\n%d", ptra+1); // so this prints x+4, cuse that's the size of int
                                return 0;
                                } //we can use other operator too. 
                        
                    

Output

3456780
3456784

Arrays and Pointers

arr[i] using pointer arithmetic

  • arr[i] == *(arr+i)
  • If arr is a pointer to arr[0] then arr + i is a pointer to arr[i]