c pointers with programming examples


Pointers:
It is a variable that represents the location of a data item.
It can be declared as a variable or an array.
They can be used to pass information back and forth between the calling
and its reference point.
They provide a way to return multiple data items from a function via
function arguments.
They permit references to other functions to be specified as arguments to a
given function.
They are closely associated with arrays and provide an alternate way to
access individual array elements.
They provide a convenient way to represent multi dimensional arrays.
They Allow a single multidimensional array to be replaced by a lowerdimensional
array of pointers, which permits a group of strings to be
represented within a single array, where the individual strings may differ in
length.
Fundamental Concepts:
Every data item declared occupies one or more continous memory cells in
computer memory.
The number of memory cells required to store data item depends on the
type of the data item.
For every identifier that is declared the compiler automatically assigns
memory cells.
The data item can be accessed if we know the location of the first memory
cell as within a computer the memory cells are numbered consequently.
The number associated with each memory cell in known as memory cells
address.
The address of memory location is determined by the Unary or address
operator “&”, which evaluates the address of the operand.
If the address of a memory location is assigned to another variable, then
that variable is termed as POINTER.
A POINTER always holds the ADDRESS of the MEMORY LOCATION but not
the value.
Therefore the memory location which is holding the address of another
memory location is called as pointer variable.
Address
of X
Value of
X
Y X
Y is a pointer variable to X’s location
The data represented by the variable ‘x’ can be accessed by the expression
*y where “*” is a UNARY OPERATOR, which is called as INDIRECTION
OPERATOR, which is used to operate only on a POINTER VARIABLE.
The ADDRESS OPERATOR “&” can act upon operands that are associated
with UNIQUE ADDRESSES therefore it cannot operate upon ARITHMETIC
EXPRESSIONS.
The INDIRECTION OPERATOR “*” can act only upon the OPERANDS that
are POINTERS(pointer variables).
Therefore an indirect reference can appear in place of any ordinary variable.
Illustration 1.
Program
# include <stdio.h>
void main ( )
{
int x = 10;
int *y;
y = &x;
printf("\n\nValue of x : %d", x);
printf("\n\nAddress of x : %u", y);
printf("\n\nAddress of x : %u", &x);
printf("\n\nValue of y : %u", y);
printf("\n\nValue of y points to : %d", *y);
}

Illustration 2.
Program
# include <stdio.h>
void main ( )
{
char ch = 'A';
char *ptr;
ptr = &ch;
printf("\n\nValue of ch : %c", ch);
printf("\n\nAddress of ch : %u", ptr);
printf("\n\nAddress of ch : %u", &ch);
printf("\n\nValue of ptr : %u", ptr);
printf("\n\nValue of ptr points to : %c", *ptr);
}
Expression in Pointers 1:
Program
# include <stdio.h>
void main( )
{
int a, b, sum;
int *x, *y;
x = &a;
y = &b;
printf("\nEnter Any Two Integers : ");
scanf("%d%d", &a, &b);
sum = (*x) + (*y);
printf("\n\nThe Sum of %d and %d is %d.", *x, *y, sum);
if(sum == (a + b))
printf("\nThe Addition Through Pointers is Successful.");
else
printf("\nThe Addition Through Pointers is Unsuccessful");
}
Expression in Pointers 2:
Program
#include<stdio.h>
void main()
{
int a, b, *p1, *p2, choice;
p1 = &a;
p2 = &b;
do
{
printf("\n1. Add \n2. Multiply \n0. Exit");
printf("\nEnter Your Choice [0..2] : ");
scanf("%d", &choice);
if(choice == 1)
{
printf("\nEnter Any Two Numbers : ");
scanf("%d%d", &a, &b);
printf("\nResult : a + b = %d", *p1 + *p2);
}
if(choice == 2)
{
printf("\nEnter Any Two Numbers : ");
scanf("%d%d", &a, &b);
printf("\nResult : a * b = %d", *p1 * *p2);
}
}while(choice > 0 && choice <= 2);
}
Passing Pointers to a Function
Pointer can be passed to a function as arguments.
This facilitates the data items within the calling portion of the program to be
accessed by the function altered within the function, and then retrieved to
the calling portion of the program in attired form.
This way of pointer usage is called passing arguments by reference /
address / location.
When the argument is passed by reference, the address of the data item is
passed to the function.
The contents of that address can be accessed freely, either within the
function or within the calling routine
Any change made to the data item will be recognized in both the function
and the calling function.
Therefore the use of a pointer as function arguments permits the
corresponding data item to be altered globally from within the function.
Points to Note:
An asterisk must precede each formal pointer arguments.
Function prototypes are declared in the same manner.
If the function declaration does not include variable names, even then an
asterisk must follow the data types of each pointer argument.
Whenever we have the duplicate values of data it is termed as CALL BY
VALUE OF DATA.
Whenever the change to a variable is done out of the functional location,
using its address it is called as CALL BY REFERENCE.

Illustration
Program
#include<stdio.h>
void main( )
{
void swap(int *, int *y);
int a = 10, b = 20;
printf ("\nBefore Swapping : a = %d, b = %d", a, b);
swap (&a, &b);
printf ("\nAfter Swapping : a = %d, b = %d", a, b);
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
Call by Value and Call by Reference:
Program
#include<stdio.h>
void main( )
{
void fun(int, int *y);
int a = 3, b = 5;
printf("\nBefore The Function is Called a : = %d, b : = %d", a, b);
fun(a, &b);
printf("\nAfter The Function is Called a : = %d, b : = %d", a, b);
}
void fun(int x, int *y)
{
x++;
(*y)++;
return;
}
In the above program, the functional arguments to function are data and a
variable address.
By using the above function we updated the values of x.
The value of variable b was updated using its address location.
Whenever some changes takes place under the sub function the original
values of actual parameter never changes.
Whenever some change takes place under the sub function for which
address is passed the original values of the actual parameter also changes.
Arrays and Pointers
Finding Smallest and largest element in array:
Program
#include<stdio.h>
void main()
{
int k, n, small, large, A[10], *p;
printf ("\nEnter The Array Elements : ");
for(k = 0, n = 10; k < n; k++)
{
printf ("\nEnter A[%d] Index Element : ", k);
scanf ("%d", &A[k]);
}
printf ("\nProcessing The Array...");
p = &A[0];
small = large = *p;
for(k = 0; k < n; p++, k++)
{
if(small > *p)
small = *p;
if (large < *p)
large = *p;
}
printf ("\n\nThe Smallest Element is %d, The Largest Element is %d", small, large);
}

No comments: