Pointers



Well pointers are the most important part in c programming and it is very important to understand pointers because they are very confusing , so lets get started
Pointers  can be defined as variables that stores the address of other variable

Address means the name of the location on the memory where we store our variable for example

int x = 10;

so here we have declared a integer variable called x which contain the value of 10 and lets say the memory address if it is 123



In the above diagram lets consider the three box as memory and x = 5 is the box where we have stored our value having the address of 123 , but in real life also we can find the address of a variable with this piece of code

main()
{
int x = 10;
printf("address of x is %d",&x);
getch();

We have stored our variable , given it a value and also find its address

pointers are also variables that stores the address of other variables  and also have the power of manipulating them



In the above diagram we have declared a pointer “ptr” which contains the address on int x =10 , we can also use pointers to change the  value of x  , so let us see the syntax of how  to use pointers

int x;
int *ptr;
ptr  = &x;
*ptr = 12;

So let us examine the steps

int x;  = in this step we just declared a integer without any value to in

int *ptr;  = this is how we declare a pointer , notice the star symbol ,  if you remove the "*"  then ptr will become just an integer , so "*" means , it is pointer , there is int before the pointer because this pointer is going to store the address of a integer , if int x was float x then then the pointer will be float and so on

ptr  = &x; = we have declared the integer and also the pointer but in order to get the address of x and store it in the pointer “ptr” we use this statement  , &x means the address of x which we are storing in ptr

*ptr = 12;  = this means that the thing the pointer is pointing to change it to the value of 12 , in other words now x from 10 is changed to12 by using a pointer , the above steps in a code looks like this

main()
{
int a;

int *ptr;

ptr = &a;

*ptr = 123;

printf(" value of a is %d\n\n",a);

printf(" value of a is %d",*ptr);

getch();                    
}

We can also use pointers as function parameter for example

#include<stdio.h>


double_it(int *x) //pointer to accept the address of y
{
*x = *x * 2; //thing the pointer points to (that is the VALUE of y)
}            // = thing the pointer points to (VALUE of y) * 2

main()
{
int y;

printf("enter number \n");
scanf("%d",&y);

double_it(&y);  //pass address of y

printf(" double is %d",y);

fflush(stdin);

getch();
}

In the above code we ask the user to give a number which we store in the variable y , we then pass the address of y (&y) to the function  double_it  which only accepts pointers as a parameter  *x , we then multiply the contents of the pointer which the pointer is pointing to  by 2 and modify the content , and then we display y

To Know More On Pointers Click Here

1 comment: