Pointers
Pointers
⚫ Pointers are undoubtedly one of the most distinct
and exciting features in C.
⚫ It has added power and flexibility to the language.
⚫ Although they are confusing and difficult to
understand for a beginner, they are a powerful
tool and handy to use once they are mastered.
Benefits of Pointers
⚫ More efficient in handling arrays and data tables.
⚫ Returning more than one value from a function.
⚫ Accessing dynamically allocated memory.
⚫ Facilitating passing of functions as arguments to other functions.
⚫ Implementing data structures like linked lists, trees and graphs.
⚫ Reduce length and complexity of programs.
⚫ Increase the execution speed and thus reduce execution time.
Pointers
⚫ Pointers are variables that contains a memory
addresses as their values.
⚫ It’s value is also stored in the memory in another
location.
⚫ They may be different every-time we run the
program.
Variable value address
quantity 179 5000
p 5000 5048
Concept of Address and Pointers
⚫ Memory can be conceptualized Contents1
ADDR1
as a linear set of data locations. ADDR2
ADDR3
ADDR4
⚫ Variables reference the ADDR5
contents of locations. ADDR6
*
*
⚫ Pointers have a value of the *
Contents1
address of a given location ADDR11 1
*
*
Contents1
ADDR16 6
Declaration of Pointer variable
In C, every variable must be declared for its type. Since pointer
variables contain addresses that belong to a separate data type,
they must be declared as pointers before we use them. The
declaration of pointer takes the following form:
data_type *pt_name;
The asterisk(*) tells the compiler that pt_name is a pointer
variable.
Examples of pointer declarations:
int *a;
float *b;
char *c;
Pointer Declaration
⚫ Consider the statements:
#include <stdio.h>
main ( )
{
int *aptr ; /* Declare a pointer to an int */
float *bptr ; /* Declare a pointer to a float */
int a ; /* Declare an int variable */
float b ; /* Declare a float variable */
}
Accessing of Pointer variable
The actual location of a variable in the memory is
system dependent and therefore, the address of a
variable is not known to us immediately. This can
be done with the help of the operator “&” available
in C. The operator & immediately preceding a
variable returns the address of the variable
associated with it. For example, the statement
P = &quantity
would assign the address 5000 (assuming) to the
variable p.
Use of & and *
⚫ When is & used?
⚫ When is * used?
⚫ & -- "address operator" which gives or produces the
memory address of a data variable.
⚫ * -- "dereferencing operator" which provides the contents
in the memory location specified by a pointer.
Use of &
#include<stdio.h>
main()
{
char a='A';
int id=44;
float gpa=3.87;
printf("address of id is %u\n", &id);
printf("address of gpa is %u\n", &a);
printf("address of cgpa is %u\n", &gpa);
}
Use of &
⚫ The & operator can only be used with a simple variable or
an array element.
⚫ The address operator cannot be used with a constant or an
expression.
⚫ &j; /* valid
⚫ &arr[i]; /* Valid
⚫ &289; /* Invalid
⚫ &(x+y); /* Invalid
Accessing of Pointer variable
#include<stdio.h>
main()
{
int x,*y,z;
x=1;
y=&x;
z=*y;
printf("value of x is %d \n",x);
printf("value of y is %u \n",y);
printf("value of z is %d \n",z);
printf("value of *y is %d \n",*y);
printf("value of *&x is %d \n",*&x);
}
Illustration of pointer assignments
#include<stdio.h>
main()
{
int x,y,*z;
x=44;
y=40;
z=&x;
printf("x=%d\t y=%d\t z=%u\n", x, y, z);
y=*z;
*z=10;
printf("x=%d\t y=%d\t z=%u\n", x, y, z );
} The ouput is
Illustration of pointer assignments
Declaration x y z
X = 44; y = 44 40
40;
address 2293416 …………… …………….
…
Z= &x 44 40 2293416
address 2293416 …………… …………….
…
Y = *z 44 44 2293416
address 2293416 …………… …………….
…
*z = 10 10 44 2293416
address 2293416 …………… …………….
…
Illustration of pointer assignments
#include<stdio.h>
main()
{
int x,y,*z;
x=44;
y=40;
z=&x;
printf("x=%d\t y=%d\t z=%u\n", x, y, z);
*z=10;
y=*z;
printf("x=%d\t y=%d\t z=%u\n", x, y, z );
What will be the output??
X=44 Y=40 Z=2293412
X=10 Y=10 Z=2293412
Pointer to pointer
⚫ Multiple Indirections or Chain of pointers:
The syntax of declaring pointer to pointer is:
data type **ptr; **ppa
*(*ppa)
int a=5; *pa
int *pa= &a; a
int **ppa = &pa;
Pointer to pointer
Program 4:
#include<stdio.h>
void main()
{
int a=5;
int *pa;
int **ppa;
pa=&a;
ppa=&pa;
printf(“Address of a =%u\n",&a);
printf(“value of pa= Address of a =%u\n",pa);
printf(“value of *pa= value of a =%u\n",*pa);
printf(“value of **ppa= valueof a =%u\n",**ppa);
}
Pointers and Arrays
Program 5:
#include<stdio.h>
int main()
{
int i,*r,odd[5]={1,3,5,7,9};
r=odd;
for(i=0;i<5;i++)
{
printf("%d\n",*r);
r++;
}
return 0;
}
Program to sum of all elements stored in an array
Program 6:
#include<stdio.h>
int main()
{
int i,*r,odd[5]={1,3,5,7,9},sum=0;
r=odd;
for(i=0;i<5;i++)
{
sum=sum+*r;
r++;
}
printf(“total=%d”,sum);
return 0;
}
Pointer and character strings
Program 8:
#include<stdio.h>
void main()
{
char *name;
int length;
name=“GOOD”;
char *cptr=name;
printf(“%s\n”,name);
while(*cptr!=‘\0’)
{
printf(“ %c stored in address %u ”,*cptr, cptr);
cptr++;
}
length= cptr-name;
printf(“Length=%d”,length);
}
Swap Function
#include <stdio.h> void swap( int a, int b )
{
void swap ( int a, int b ) ;
int temp;
int main ( )
temp= a; a= b; b = temp ;
{ printf ("a=%d b=%d\n", a, b);
int a = 5, b = 6; }
printf("a=%d b=%d\n",a,b) ; Results:
swap (a, b) ; a=5 b=6
printf("a=%d b=%d\n",a,b) ; a=6 b=5
return 0 ; a=5 b=6
}
Pointers and Functions
⚫ Pointers can be used to pass addresses of variables to
called functions, thus allowing the called function to alter
the values stored there.
⚫ We looked earlier at a swap function that did not change
the values stored in the main program because only the
values were passed to the function swap.
⚫ This is known as "call by value".
Pointers and Functions
⚫ If instead of passing the values of the variables to the called function,
we pass their addresses, so that the called function can change the
values stored in the calling routine. This is known as "call by
reference" since we are referencing the variables.
⚫ The following shows the swap function modified from a "call by value"
to a "call by reference". Note that the values are now actually
swapped when the control is returned to main function.
Category 5: Functions that return multiple values.
It is possible to return multiple values to the calling function. C
uses the arguments not only to receive information but also to
send back information to the calling function. The arguments
that are used to send out information are called output
parameters. The mechanism of sending back information
through arguments is achieved using what is known as the
address operator (&) and indirection operator (*).
Pointers with Functions (example)
#include <stdio.h> void swap( int *a, int *b )
void swap ( int *a, int *b ) ; {
int main ( ) int temp;
{ temp= *a; *a= *b; *b = temp ;
printf ("a=%d b=%d\n", *a, *b);
int a = 5, b = 6;
}
printf("a=%d b=%d\n",a,b) ;
Results:
swap (&a, &b) ; a=5 b=6
printf("a=%d b=%d\n",a,b) ; a=6 b=5
return 0 ; a=6 b=5
}
Returning more than one value in function
#include<stdio.h>
func(int x, int y, int *ps, int *pd, int *pp);
void main()
{
int a, b, sum, diff, prod;
a=6; b=4;
func(a, b, &sum, &diff, &prod);
printf(“sum= %d difference=%d product=%d”, sum, diff, prod);
}
func(int x, int y, int *sum, int *diff, int *prod)
{
*sum = x+y;
*diff = x- y;
*prod = x*y;
}
Factorial program in c using pointers
#include<stdio.h> void findFactorial(int num,int
*factorial){
void findFactorial(int,int *); int i;
int main(){ *factorial =1;
int i,factorial,num;
for(i=1;i<=num;i++)
printf("Enter a number: "); *factorial=*factorial*i;
scanf("%d",&num); }
findFactorial(num,&factorial);
printf("Factorial of %d is: %d",num,factorial);
return 0;
}
Find the largest Number in c using pointers
#include <stdio.h>
larger(int a, int b, int *large);
void main()
{
int a, b, large;
printf("Enter two numbers to find the larger: ");
scanf("%d%d",&a,&b);
larger(a, b, &large);
printf("%d is larger",large);
}
int larger(int a, int b, int *large)
{
if(a > b)
return *large =a;
else
return *large=b;
}
?