C Pointers

Pointers: Pointers is nothing but the memory address of the any variable. When we declare any of variable it takes some memory , the address of this memory is called pointer. Pointers in C are very easy and fun to learn. Some C programming tasks are performed more easily with the pointers, and other tasks, such as the dynamic memory allocation, cannot be performed without using pointers. So it is very necessary to learn the pointers to become a perfect C programmer. We can easily find the memory address using & operator.

#include<stdio.h>
int main(){
    int a,b;
    printf("%\n",&a);
    printf("%\n",&b);

    return 0;
  }
Output:
6356725
6356728   

Declaration of pointer variable:
data_type *variable_name;
//see following example
int    *a;/* pointer to an integer */
double *b;/* pointer to a double */
float  *c;/* pointer to a float */
char   *d;/* pointer to a character */

You must remember that * sign always point the value of particular memory address.

#include<stdio.h>
int main(){
  int *p;
  int v;
  v = 5;
  
  p = &v//insert the address v 
  printf("%d\n",&v);//address of v
  printf("%d\n",&p);//address of pointer
  printf("%d\n",p);//inserted address of v 
  printf("%d\n",*p);// * sign point the value  

  return 0;
}
Output:
  6356732
  6356734
  6356732
  5

[Note:Address will be changed each time you run the code]

Let's analize the above code. First line we have declare pointer variable. We know that every variable have a address so *p and v have also different address and 6356734 is the address of pointer p and 6356732 is the address of variable v . Then v = 5; it means that in the address of 6356732 we put 5 .Then we written p = &v; it means we insert the address of v 6356732 in the pointer address(6356734) as a value . Now if you print p it will show the address of v because of it holds the address of v. So when we print &v it shows 6356732 , then for &p it shows 6356734 , for p it shows 6356732 then finally when we print *p it point the value in the address of 6356732 and it shows 5.

1. Pointer arithmetic:

We can perform arithmetic operation with pointer because memory address is a numeric value . There are four arithmetic operation that we can perform with pointer these are +, -, ++, -- . If we add 1 with a pointer variable it will change accroding to its data type . Let's Assume P is an int type pointer variable and it holds 1000 address if we add 1 with P the address become 1004 because int takes 4 bytes of memory if it is an double type pointer variable then it will become 1008 because double takes 8 bytes of memory and it will do the same with rest of the operators as well . Let's see the following example:

#include<stdio.h>
int main(){
  int *p, v;
  p = &v;
  printf("%d\n",p);
  p = p+1; //same as p++
  printf("%d\n",p);
  return 0;
}
Output:
  6356720
  6356724

2. Array of pointers:

Array pointer is same as normal array but an array pointer reffers to an address and array reffers to a value. Let's see the example:

#include<stdio.h>
int main(){
  int *p[3], v[3]= {10,20,30};
  for(int i = 0;i++){
   p[i] = &v[i];
  }
  for(int i = 0;i++){
    printf("%d ",*p[i]);
  }
  return 0;
}
Output:
  10 20 30 

3. Pointer to pointers:

When we deal with pointer to pointer we must use ** instead of single *. Let's see the example:

#include<stdio.h>
int main(){
  int v = 3; //v reffer to value
  int *p;
  int **ptp; //use for pointer to pointer
  p = &v;
  ptp = &p;
  printf("%d\n",v);
  printf("%d\n",*p);
  printf("%d\n",**ptp);
  
  return 0;
}
Output:
  3
  3
  3

Let's analize the above code and assume address of v is 1000 and address of &p is 2000 that means p or 2000 hold 1000 and *(1000) is 3. Side by side address of the pointer variable is 2000 and ptp hold 2000 So, **ptp is *(*(2000)) but *(2000) is 1000 so ptp becomes *(1000) which is 3.

Best way of learing

Self learing is the best learing in the world. Discover yourself first then will get what you are And what you want to do .It will push you for self learing.

Why you need to learn coding?

Coding will play a vital role in one's life . It will help to open a new window of thinking . You can think better way than past . It helps to organise all the thing in better way .