Pointers in C
Pointers in C
Shailaja K.P
Introduction
Whenever a variable is declared, system will allocate a location to that
variable in the memory, to hold value.
This location will have its own address number.
Let us assume that system has allocated memory location 2686778
for a variable a.
int a = 10 ;
value 10
2686778 address
We can access the value 10 by either using the variable name a or the address
2686778
Since the memory addresses are simply numbers they can be assigned to
some other variable.
The variable that holds memory address are called pointer variables.
A pointer variable is therefore nothing but a variable that contains an address,
which is a location of another variable.
Value of pointer variable will be stored in another memory location.
address of “a”
2686778
pointer ptr pointer name
382542
address of pointer
10
a
Declaring a pointer variable
General syntax of pointer declaration is,
data-type *pointer_name;
Data type of pointer must be same as the variable, which the pointer is
pointing.
Invalid Initialization
float a;
int *ptr;
ptr = &a; //ERROR, type mismatch
Example program
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf(“%u”, ptr);
printf("%d", *ptr);
return 0;
}
Pointer and Arrays
When an array is declared, compiler allocates sufficient amount of memory to
contain all the elements of the array.
Base address which gives location of the first element is also allocated by the
compiler.
Suppose we declare an array arr,
int arr[5]={ 1, 2, 3, 4, 5 };
Assuming that the base address of arr is 1000 and each integer requires two
byte, the five element will be stored as follows
int *p;
p = arr; or p = &arr[0]; //both the statements are equivalent.
Now we can access every element of array arr using p++ to move from
one element to another.
Pointer to Array
we can use a pointer to point to an Array, and then we can use that pointer to
access the array.
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i=0; i<5; i++)
{
printf("%d", *p); p++;
}
In this program, the pointer *p will print all the values stored in the array one by
one
•Program to add two numbers using pointers
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}
Pointers and functions
Types of function calls in C
Functions are called by their names.
If the function is without argument, it can be called directly using its
name.
But for functions with arguments, we have two ways to call them,
Call by Value
Call by Reference
Call by Value
In this calling technique we pass the values of arguments which are
stored or copied into the formal parameters of functions.
Hence, the original values are unchanged only the parameters inside
function changes
But we can change this program to modify the original x, by making the
function calc() return a value, and storing that value in x.
Output : 20
void calc(int x);
int main()
{
int x = 10;
calc(x);
printf("%d", x);
}
void calc(int x)
{
x = x + 10 ;
}
Output: 10
In this case the actual variable x is not changed, because we pass argument by value,
hence a copy of x is passed, which is changed, and that copied value is destroyed as the
function ends.
So the variable x inside main() still has a value 10.
Pointer as Function parameter
Pointer in function parameter list is used to hold address of argument
passed during function call.
This is also known as call by reference.
When a function is called by reference any change made to the
reference variable will effect the original variable.
Call by Reference
In this we pass the address of the variable as arguments.
In this case the formal parameter can be taken as a reference or a
pointer, in which case they will change the values of the original
variable.
void calc(int *p);
int main()
{
int x = 10;
calc(&x); // passing address of x as argument
printf("%d", x);
}
Output : 20
Program to swap two values
#include <stdio.h>
void swap(int * q,int * p)
{
int temp = *p;
*p = *q;
*q = temp;
}
int main()
{
int a = 10, b = 2, x = 3, y = 5;
printf("a,b,x,y: %d,%d,%d,%d\n", a, b, x, y);
swap(&x, &y);
swap(&a, &b);
printf("a,b,x,y: %d,%d,%d,%d\n", a, b, x, y);
}
Program to computes the area and perimeter of a rectangle:
#include <stdio.h>
void rectangle(int a, int b, int * area, int * perim);
int main()
{
int x, y;
int area, perim;
printf("Enter two values : " );
scanf("%d %d", &x, &y);
rectangle(x, y, &area, &perim);
printf("Area is %d Perimeter is %d\n", area, perim);
return 0;
}
void rectangle(int a,int b,int * area,int * perim)
{
*area = a * b;
*perim = 2 * (a + b);
}