0% found this document useful (0 votes)
35 views

Pointer

Pointer is a variable that stores the address of another variable. Pointers allow access and manipulation of memory locations. Pointer arithmetic allows incrementing or decrementing a pointer to access memory addresses sequentially. An array of pointers allows storing addresses of other variables in an array. This allows accessing variables indirectly through their addresses stored in the array of pointers.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Pointer

Pointer is a variable that stores the address of another variable. Pointers allow access and manipulation of memory locations. Pointer arithmetic allows incrementing or decrementing a pointer to access memory addresses sequentially. An array of pointers allows storing addresses of other variables in an array. This allows accessing variables indirectly through their addresses stored in the array of pointers.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Pointers

Syllabus

 Pointer
 pointer operator
 Pointer expression
 Declaration of pointer
 Initializing pointer
 de-referencing
 Pointer to pointer
 Constant pointer
 Array of pointers
 Pointer to function.
Pointers
 A pointer is a variable whose value is the address of another variable.

 Like any variable or constant, you must declare a pointer before you can
work with it. The general form of a pointer variable declaration is −

type *var-name;

type is the pointer's base type; it must be a valid C type

var-name is the name of the pointer variable


How to use a pointer?

 Establish a pointer variable.

 Employing the unary operator (&), which yields the address of the
variable, to assign a pointer to a variable's address.

 Using the unary operator (*), which gives the variable's value at the
address provided by its argument, one can access the value stored in
an address.
Since the data type knows how many bytes the information is held in, we associate
it with a reference. The size of the data type to which a pointer points is added when
we increment a pointer.
Advantage of pointer

 Pointer reduces the code and improves the performance, it is used to


retrieving strings, trees etc. and used with arrays, structures and
functions.

 We can return multiple values from function using pointer.

 0 It makes you able to access any memory location in the


computer's memory.
Symbols used in pointer

Symbol Name Description

& (ampersand sign) Address operator Determine the address of a variable.


∗ (asterisk sign) Indirection operator Access the value of an address.
#include <stdio.h>

int main() {
int myAge = 43;

printf("%d\n", myAge);
printf("%p\n", &myAge);
return 0;
}

Output

43
0x7ffe5367e044
#include <stdio.h>

int main() { Output


int myAge = 43;
43
int* ptr = &myAge; 0061FF18
0061FF18
printf("%d\n", myAge);
printf("%p\n", &myAge);
printf("%p\n", ptr);
return 0;
}
#include <stdio.h>
int main()
{
int myAge = 43;
Output
int* ptr = &myAge;
0061FF18
printf("%p\n", ptr); 43
printf("%d\n", *ptr);
return 0;
}
Call by Value or Call by Reference

 Functions can be invoked in two ways: Call by Value or Call by


Reference. These two ways are generally differentiated by the
type of values passed to them as parameters.

 The parameters passed to the function are called actual


parameters whereas the parameters received by the function are
called formal parameters.
Call By Value

 In call by value method of parameter passing, the values of


actual parameters are copied to the function’s formal
parameters.

 There are two copies of parameters stored in different memory


locations.

 One is the original copy and the other is the function copy.

 Any changes made inside functions are not reflected in the


actual parameters of the caller.
#include <stdio.h>
void swapx(int x, int y);
int main()
{
int a = 10, b = 20;

swapx(a, b);

printf("In the Caller:\na = %d b = %d\n", a, b);

return 0;
}
void swapx(int x, int y)
{
int t;
t = x;
x = y;
y = t;

printf("Inside Function:\nx = %d y = %d\n", x, y);


}
Inside Function:

x = 20 y = 10

In the Caller:

a = 10 b = 20
Call by Reference

 In call by reference method of parameter passing, the address of the actual


parameters is passed to the function as the formal parameters.

 Both the actual and formal parameters refer to the same locations.

 Any changes made inside the function are actually reflected in the actual
parameters of the caller.
#include <stdio.h>
void swapx(int*, int*);
int main()
{
int a = 10, b = 20;

swapx(&a, &b);

printf("Inside the Caller:\na = %d b = %d\n", a, b);

return 0;
}
void swapx(int* x, int* y)
{
int t;

t = *x;
*x = *y;
*y = t;

printf("Inside the Function:\nx = %d y = %d\n", *x, *y);


}
Output
Inside the Function:
x = 20 y = 10
Inside the Caller:
a = 20 b = 10
Call By Value Call By Reference

While calling a function, instead of passing the values of


While calling a function, we pass the values of variables
variables, we pass the address of variables(location of
to it. Such functions are known as “Call By Values”.
variables) to the function known as “Call By References.

In this method, the value of each variable in the calling In this method, the address of actual variables in the
function is copied into corresponding dummy variables calling function is copied into the dummy variables of
of the called function. the called function.

With this method, the changes made to the dummy With this method, using addresses we would have access
variables in the called function have no effect on the to the actual variables and hence we would be able to
values of actual variables in the calling function. manipulate them.

In call-by-values, we cannot alter the values of actual In call by reference, we can alter the values of variables
variables through function calls. through function calls.

Pointer variables are necessary to define to store the


Values of variables are passed by the Simple technique.
address values of variables.

This method is preferred when we have to pass some This method is preferred when we have to pass a large
small values that should not change. amount of data to the function.
C - Pointer arithmetic

• A pointer in c is an address, which is a numeric value. Therefore,


you can perform arithmetic operations on a pointer just as you can
on a numeric value.
• There are four arithmetic operators that can be used on pointers: ++,

--, +, and –
• To understand pointer arithmetic, let us consider that ptr is an integer
pointer which points to the address 1000.
ptr++
• After the above operation, the ptr will point to the location
1004 because each time ptr is incremented, it will point to the
next integer location which is 4 bytes next to the current
location.
• This operation will move the pointer to the next memory
location without impacting the actual value at the memory
location.
• If ptr points to a character whose address is 1000, then the
above operation will point to the location 1001 because the
#include <stdio.h>

const int MAX = 3;

int main ()
{

int var[] = {10, 100, 200};


int i, *ptr;

/* let us have array address in pointer */


ptr = var;

for ( i = 0; i < MAX; i++) {

printf("Address of var[%d] = %x\n", i, ptr );


printf("Value of var[%d] = %d\n", i, *ptr );

/* move to the next location */


ptr++;
}

return 0;
}
Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200
#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr;

/* let us have array address in pointer */


ptr = &var[MAX-1];

for ( i = MAX; i > 0; i--) {

printf("Address of var[%d] = %x\n", i-1, ptr );


printf("Value of var[%d] = %d\n", i-1, *ptr );

/* move to the previous location */


ptr--;
}

return 0;
}
Address of var[2] = bfedbcd8
Value of var[2] = 200
Address of var[1] = bfedbcd4
Value of var[1] = 100
Address of var[0] = bfedbcd0
Value of var[0] = 10
C - Array of pointers

Before we understand the concept of arrays of pointers, let us consider the following example,
which uses an array of 3 integers −

#include <stdio.h>
const int MAX = 3
int main ()
{
int var[] = {10, 100, 200};
int i;
for (i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, var[i] );
}
return 0;
}
When the above code is compiled and executed, it produces
the following result −

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
• There may be a situation when we want to maintain an
array, which can store pointers to an int or char or any
other data type available.
• Following is the declaration of an array of pointers to an
integer −

It declares ptr as an array of MAX integer pointers. Thus, each


int *ptr[MAX];
element in ptr, holds a pointer to an int value.
#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr[MAX];

for ( i = 0; i < MAX; i++) {


ptr[i] = &var[i]; /* assign the address of integer. */
}

for ( i = 0; i < MAX; i++) {


printf("Value of var[%d] = %d\n", i, *ptr[i] );
}

return 0;
}
When the above code is compiled and executed, it produces
the following result −

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
#include <stdio.h>
const int MAX = 4;
int main () {

char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali"
};

int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i] );
}

return 0;
}
When the above code is compiled and executed, it produces the following
result −

Value of names[0] = Zara Ali


Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali
C - Pointer to Pointer

• A pointer to a pointer is a form of multiple indirection, or a chain of pointers.


• Normally, a pointer contains the address of a variable.
• When we define a pointer to a pointer, the first pointer contains the address of the second pointer,
which points to the location that contains the actual value as shown below.
• A variable that is a pointer to a pointer must be
declared as such. This is done by placing an additional
asterisk in front of its name.
• For example, the following declaration declares a
pointer to a pointer of type int −

int **var;
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}
When the above code is compiled and executed, it produces
the following result −

Value of var = 3000


Value available at *ptr = 3000
Value available at **pptr = 3000
Function Pointer

• As we know that we can create a pointer of any data type such


as int, char, float, we can also create a pointer pointing to a
function.
• The code of a function always resides in memory, which means
that the function has some address. We can get the address of
memory by using the function pointer.
#include <stdio.h>
int main()
{
printf("Address of main() function is
%p",main);
return 0;
}
Declaration of a function pointer

• Till now, we have seen that the functions have addresses, so


we can create pointers that can contain these addresses, and
hence can point them.
Syntax of function pointer
return type (*ptr_name)(type1, type2…);
For example:
int (*ip) (int);
• In the above declaration, *ip is a pointer that points to a
function which returns an int value and accepts an integer
value as an argument.

• float (*fp) (float);


• In the above declaration, *fp is a pointer that points to a
Till now, we have learnt how to declare the function pointer.
Our next step is to assign the address of a function to the
function pointer.

float (*fp) (int , int); // Declaration of a function pointer.


float func( int , int ); // Declaration of function.
fp = func; // Assigning address of func to the fp
pointer.
Calling a function through a function pointer
• We already know how to call a function in the usual way.
Now, we will see how to call a function using a function
pointer.
Suppose we declare a function as given below:
float func(int , int); // Declaration of a function.
Calling an above function using a usual way is given below:

result = func(a , b); // Calling a function using usual ways.


Calling a function using a function pointer is given below:

result = (*fp)( a , b); // Calling a function using


function pointer.
Or
result = fp(a , b); // Calling a function using function
pointer, and indirection operator can be removed.
The effect of calling a function by its name or function
pointer is the same. If we are using the function pointer, we
can omit the indirection operator as we did in the second
#include <stdio.h>
int add(int,int);
int main()
{
int a,b;
int (*ip)(int,int);
int result;
printf("Enter the values of a and b : ");
scanf("%d %d",&a,&b);
ip=add;
result=(*ip)(a,b);
printf("Value after addition is : %d",result);
return 0;
}
int add(int a,int b)
{
int c=a+b;
return c;
}
Array of Function Pointers
Function pointers are used in those applications where we do
not know in advance which function will be called. In an
array of function pointers, array takes the addresses of
different functions, and the appropriate function will be
called based on the index number.
#include <stdio.h>
float add(float,int);
float sub(float,int);
float mul(float,int);
float div(float,int);
int main()
{
float x; // variable declaration.
int y;
float (*fp[4]) (float,int); // function pointer declaration.
fp[0]=add; // assigning addresses to the elements of an array of a function pointer.
fp[1]=sub;
fp[2]=mul;
fp[3]=div;
printf("Enter the values of x and y :");
scanf("%f %d",&x,&y);
float r=(*fp[0]) (x,y); // Calling add() function.
printf("\nSum of two values is : %f",r);
r=(*fp[1]) (x,y); // Calling sub() function.
printf("\nDifference of two values is : %f",r);
r=(*fp[2]) (x,y); // Calliung sub() function.
printf("\nMultiplication of two values is : %f",r);
r=(*fp[3]) (x,y); // Calling div() function.
printf("\nDivision of two values is : %f",r);
return 0;
}
float add(float x,int y)
{
float a=x+y;
return a;
}
float sub(float x,int y)
{
float a=x-y;
return a;
}
float mul(float x,int y)
{
float a=x*y;
return a;
}
float div(float x,int y)
{
float a=x/y;
return a;
}
Return pointer from functions
int * myFunction() {
.
.
.
}

• it is not a good idea to return the address of a local variable


outside the function, so you would have to define the local
variable as static variable.
#include <stdio.h>
#include <time.h> /* main function to call above defined function */
int main () {
/* function to generate and return random
numbers. */ /* a pointer to an int */
int * getRandom( ) { int *p;
int i;
static int r[10];
int i; p = getRandom();

/* set the seed */ for ( i = 0; i < 10; i++ ) {


srand( (unsigned)time( NULL ) ); printf("*(p + [%d]) : %d\n", i, *(p + i) );
}
for ( i = 0; i < 10; ++i) {
r[i] = rand(); return 0;
printf("%d\n", r[i] ); }
}

return r;
}
1523198053
1187214107
1108300978
430494959
1421301276
930971084
123250484
106932140
1604461820
149169022
*(p + [0]) : 1523198053
*(p + [1]) : 1187214107
*(p + [2]) : 1108300978
*(p + [3]) : 430494959
*(p + [4]) : 1421301276
*(p + [5]) : 930971084
*(p + [6]) : 123250484
*(p + [7]) : 106932140
*(p + [8]) : 1604461820
*(p + [9]) : 149169022

You might also like