Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

Pointers in C:

Pointers are what the word literally means. They point to something. That something
can be the location in the memory to a variable, an array or a structure. In object
oriented programming languages like C++, they can point to objects. For a day to day
example, a pointer can point to any file in your computer. Have you noticed that when
you cut-paste a (big)file from one drive to another, it takes some time. But when you do
the same within a drive, it happens instantaneously. It is because when you cut-paste to
a different drive, the contents(actual file) has to be moved, but when it is done within
the drive, the pointer to that file is simply redirected to the new location. Similarly
deleting a file simply means deleting the pointer to the memory location of that file. So
to completely erase data from a hard drive tech guysreset all bits to zero.
Coming back to C, let me explain the necessity of pointers through a simple program.
Consider swapping two numbers from their variables. This can be done easily within the
main() function. But suppose we need to do that to a lot of variables/multiple times in
our program. So we write a function where we pass the two variables as arguments. The
function performs the operation and program execution comes back to the main
function. So in the next line we print the variables and expect the values to be swapped

Without using pointers:


#include<stdio.h>
void swap(int,int);//Declare the function
int main()
{
int a=20, b=10;
printf(Before swapping: %d %d\n, a, b);
swap(a,b);
printf(After swapping: %d %d\n, a, b);
return 0;
}
void swap(int p,int q)
{
int temp;//Create temporary integer
temp = p;
/*p got the value of a and q got the value of b when a
and
b were passed as arguments */
p = q;// i.e. p = 20 and q = 10
q = temp;
/* values of p and q are interchanged, so p=10, q=20,
but the original values of a and b remains unaffected */
}

Now using pointers, we define pointers to those variables


and pass the address of the variables as the arguments
into the function, here is the code:
#include<stdio.h>
void swap(int*,int*);//Declare the function
int main()
{
int a=20, b=10;
int *x ,*y;//Create two pointers
x = &a;//Assign first pointer to address of(&) a
y = &b;//Assign first pointer to address of(&) b
printf(Before swapping: %d %d\n, a, b);
swap(x,y);
/* passing x and y as arguments is
same as passing &a and &b i.e. address of a and b */
printf(After swapping: %d %d\n, a, b);
return 0;
}
void swap(int *p,int *q)
{
int temp;//Create temporary integer
temp = *p;//*p represents the value stored in memory location
pointed by p
*p = *q;// So we are interchanging the values in the two memory
locations
*q = temp;
/*pointed by p and q, i.e. by x and y,
which are actually the variables a and b */
}

The first method of calling a function is calledcall by


valuewhere the values of variables are passed. The changed
made to these valuesinside the function remains valid only
within the scope of the function, the changes are not reflected
outside the function.
The secondmethod of calling a function is calledcall by
referencewhere the addressesof variables are passed. The
changed made to these valuesinside the functionaffects the
actual values of the variables in the whole program.
Pointers are used to point to strings, arrays, structures and
objects. They are very important in creating custom data
structures likelinked listsandqueues. Well discuss those
topics in future blogs.
Comment below if youwant to further discuss this topic.

Accessing the address of a variable


The actual location of a variable in the memory is
system depend and therefour , the addressing of a
variable is not know to us immediately
How can we then determine the address of a variable?
This can be done with the help of the operator &variable
in c.
The operator &immediately preceding a variable return
the address of the variable associated with it.

#include<stdio.h>

main()
{
int i = 3, *j, **k; j = &i; k = &j;
printf("\nAddress of i = %u", &i);
printf("\nAddress of i = %u", j);
printf("\nAddress of i = %u", *k);
printf("\nAddress of j = %u", &j);
printf("\nAddress of j = %u", k)
; printf("\nAddress of k = %u", &k);
printf("\nValue of j = %u", j);
printf("\nValue of k = %u", k);
printf("\nValue of i = %d", i);
printf("\nValue of i = %d", *(&i));
printf("\nValue of i = %d", *j);
printf("\nValue of i = %d", **k);
}
Output :
Address of i = 65524
Address of i = 65524
Address of i = 65524
Address of j = 65522
Address of j = 65522
Address of k = 65520
Value of j = 65524
Value of k = 65522
Value of i = 3
Value of i = 3
Value of i = 3
Value of i = 3

Variable

Actual Value

Value of i

Value of j

65524

Value of k

65522

Address of i

65524

Address of j

65522

Address of k

65520

You might also like