0 ratings0% found this document useful (0 votes) 31 views4 pagesPointers Not
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Pointers in C
Pointers are variables that used to store the addresses of other type of variables (or functions) in C.
In the adjacent figure,
‘num is an int type variable with value 26
ptris a pointer holding the address of
rum (the address of num is 1100 )
piritself is a variable (pointer type)
stored at address 1080
Image source : hitps://[Link]/e-pointers!
Addresses of variables can be obtained using the & operator (known as referencing)
and can be assigned to @ pointer type variable, Pointers also provide the operation of * (
dereferencing ) which is getting the value of the variable stored at the address contained
inside a pointer variable.
&- “Address of (Referencing ) operator, used to get the address ofa variable
* = “Value at” ( De- referencing operator) , used to get the value at the address
contained ina pointer type variable
The following code illustrates the use of the following operators using a pointer to an
int type
1} source code to store the addess of int variable
Winclude
int main()
'
int a= 100; int variable initialized to 100
int * p= 8a; I/ pointer to an integer variable
ithe pointer p holds the address ofthe variable address
printi("\n Address of ais Yeu", &a); address of address
printi("In The contents of the pointer variable
printi("in Value at pis %d", *p)
Wend mai
Scanned with CamScannerUses of Pointers
* Passing arguments to functions using call by reference
« To Retum multiple values from functions
Passing structures and arrays to functions efficiently
« Accessing array elements using base address and offset
Manipulating strings
© Accessing structure members
¢ Dynamic memory allocation
© Creating different type of data structures like lists, trees etc.
Reading and writing directly from/into some memory location
« Low level programming, Systems programming
Pointer Arithmetic
Like other data types pointers do have some specific operations defined on them. These
operations are
Increment/Decrement
Addition/Subtraction by integer
Difference of two pointers
Comparison of two pointers
.
Increment/Decrement of pointers
If we increment a pointer by 1, the pointer will start pointing to the immediate next location. This
is somewhat different from the general arithmetic since the value of the pointer will get increased
by the size of the data type to which the pointer is pointing. For e.g. in a platform with 4 byte
integer , a increment of I to an integer pointer will result in the pointer moving 4 bytes in
memory. Similarly when a character pointer is incremented by 1 , it will move I byte in memory.
The following illustrates this concept.
Scanned with CamScanner17" Code to observe the increment ina pointer when incremented by 1 */
‘nctude
fet maing)
«
Int rumber=50;
Int *p pointer to int
peAnumber/stores the address of number variable
print(Address ofp varabe Is Sou or,
pat
print(After Increment: Adéress ofp variable Is S6u \n");// In our case, p will get Incromented by 4 bytes.
return 0
>
The above code will exhibit different increment values in the pointer when depending upon the
type of the pointer.
Addition/subtraction with an integer is similar to increment/decrement, but here the changes
in the address contained in the pointer will be a multiple of the size of the data type to which
the pointer is pointing.
For e.g. if p is a pointer to an integer type ( int * ) then p + 5 will lead to p incremented by
5*sizeoiint) bytes. For a platform which provides 4 bytes integer type, p+ 4 will increment p
by 20 bytes.
Ifp is a pointer to a double type (double * ) then p +5 will lead to p incremented by 40 bytes.
Difference of two pointers
Two pointers can be subtracted when they point to elements of the same array object. The
result of the subtraction is the number of that type that can be accommodated between the two
addresses. The following code prints 100 becasse 100 integers can be accommodated between
the addresses pointed by p and q. Taking the difference of tow pointers is useful in when
accessing array elements with pointers.
Hi source code for difference between two pointers
include
int main)
(
int a[100}// array of 100 integers
int * p, *q// p and q are integer pointers
p= &aj0]: /"p pointing to the first clement of the array
&a{100];//q points to the las element ofthe array (100th )
print" ap Su", q-P),
1 prints 100 , because these addresses are seperated by 180 integers
lend of main
Comparison of pointers
Two pointers are considered equal if they are pointing to the same memory location,
‘Types of Pointers
Pointers can be of various types depending upon which data type it is pointing or which
object its is pointing. We can have pointers of the following types —
* Pointers to various primitive types such as int , char, float ete.
+ Pointers to arrays
* Pointers to pointers
* Pointers to derived types such as structures, unions etc.
* Pointers to functions
Scanned with CamScanner© void pointers ( generic type , can be typecasted to any type )
In addition to these pointers can also be classified as per the size of the address space in which
they are pointing to. Acoording to this pointers can be classified into the following types
© Near pointers (default)
Near pointer is a pointer which is used to bit address of up to 16 bits in a given
section of the computer memory that is 16 bit enabled. It can only access data of a small
size of about 64 kb in a given period, which is the main disadvantage of this.
© Far pointers
Far pointer is a 32-bit pointer, can access information which is outside the computer
memory in a given segment. To use this pointer, one must allocate his/her sector register to
store data address in the segment and also another sector register must be stored within the
most recent sector.
© Huge pointers
Huge pointer has the same size of 32-bit to that of a far pointer, and it can also
access bits that are located outside the sector. Far pointer which is fixed and hence that
part of the sector in which they are located cannot be modified in any way; huge
pointers can be.
Programs based on Pointers
Q Write a C program to swap the value of 2 integer variables using pointers.
M1 source code to swap two integer variables using pointers,
include
int main
{
int x=100 , y=200 , temp; // integer variables
int *p, *q;// pointers to integer type
p=&x: !p points tox
q=&y, [fq points toy
the values of integers before swapping
Yad and y=%d", x, y)s
Méisplay the values of integers after swapping,
printi("\n x=9%d and y=%d ", x,y):
Mend of main
Scanned with CamScanner