0% found this document useful (0 votes)
16 views16 pages

Unit 5

The document provides an overview of pointers in C programming, explaining their definition, declaration, and usage. It illustrates how pointers store memory addresses, demonstrates pointer notation, and discusses the difference between call by value and call by reference. Examples and output from various programs are included to clarify the concepts presented.

Uploaded by

ccpatil.24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

Unit 5

The document provides an overview of pointers in C programming, explaining their definition, declaration, and usage. It illustrates how pointers store memory addresses, demonstrates pointer notation, and discusses the difference between call by value and call by reference. Examples and output from various programs are included to clarify the concepts presented.

Uploaded by

ccpatil.24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PSP-I

UNIT –IV
Pointer in C

Dr. Prashant B Wakhare


Mob NO-7020880915
Mail Id: [email protected]
https://www.linkedin.com/in/pbwakhare/
Syllabus

• Introduction
• Definition and Declaration of pointers
• Address operator
• Pointer variables
What is Pointer
• A pointer is a variable that stores the memory
address of another variable as its value.
• A pointer variable points to a data
type (like int) of the same type, and is created
with the * operator.
Pointer Notation
Consider the declaration,
int i = 3 ;
This declaration tells the C compiler to:
• (a) Reserve space in memory to hold the
integer value.
• (b) Associate the name i with this memory
location.
• (c) Store the value 3 at this location.
Pointer Notation
We may represent i’s location in memory by
the following memory map.
Pointer
• We can print this address number through the following
program:

main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
Pointer
Observe carefully the output of the following program:
main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
Value of i = 3

Note that printing the value of *( &i ) is same as printing the value of i.
Example
int myAge = 43; // An int variable
int* ptr = &myAge; // A pointer variable, with the name
ptr, that stores the address of myAge
// Output the value of myAge (43)
printf("%d\n", myAge);
// Output the memory address of myAge (0x7ffe5367e044)
printf("%p\n", &myAge);
// Output the memory address of myAge with the pointer
(0x7ffe5367e044)
printf("%p\n", ptr);
Note
• Note that the * sign can be confusing here, as
it does two different things in our code:
• When used in declaration (int* ptr), it creates
a pointer variable.
• When not used in declaration, it act as
a dereference operator.
What would be the output?
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
Observe how the variables j and k
have been declared

int i, *j, **k ;


Back to Function Calls
• Arguments can generally be passed to functions in one of the two
ways:
• (a) sending the values of the arguments
• (b) sending the addresses of the arguments

In the first method the ‘value’ of each of the actual arguments in


the calling function is copied into corresponding formal arguments
of the called function. With this method the changes made to the
formal arguments in the called function have no effect on the
values of actual arguments in the calling function. The following
program illustrates the ‘Call by Value’.
sending the values of the arguments
main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
swapv ( int x, int y )
{
int t ;
t=x;
x=y;
y=t;
printf ( "\nx = %d y = %d", x, y ) ;
}
The output of the above program would be:
• x = 20 y = 10
• a = 10 b = 20

• Note that values of a and b remain unchanged even after exchanging the values of x and y.
sending the addresses of the
arguments
• In the second method (call by reference) the addresses of actual arguments
in the calling function are copied into formal arguments of the called
function. This means that using these addresses we would have an access to
the actual arguments and hence we would be able to manipulate them. The
.
following program illustrates this fact
sending the addresses of the
arguments
main( )
{
int a = 10, b = 20 ;
swapr ( &a, &b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
swapr( int *x, int *y )
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
}

• The output of the above program would be:


• a = 20 b = 10
• Note that this program manages to exchange the values of a and b using their addresses stored in x
and y.

You might also like