Pointers in C

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Pointers

Outline

1 Pointers

2 Pointer Arithmetic

3 Arrays and Pointers

4 Passing Pointers to Functions


POINTERS

Pointers are variables, which contain the address of some other


variables.
Declaration: datatype *pointername;
e.g.int * ptr;

The type of a pointer depends on the type of the variable it points to.
Every pointer points to some data type.
Sizes of basic data types

All data is stored in memory. But different data types occupy


different amount of memory.
The sizeof() operator in C can be used to determine the number of
bytes occupied by each data type.

sizeof(int) = 4
sizeof(float) = 4
sizeof(double) = 8
A SAMPLE
PROGRAM
#include <stdio.h>
0 int
main()
1
{
2 int n;
3 char c;
4 X c int *ptrn;
c=’X’;
.. n=15;
ptrn=&n;
232 − 1
return 0;
}
Memory Layout (Bytes)
0
1
2 #include <stdio.h>
3
int main()
4 X c
{
char c;
.. int n;
int *ptrn;
20 0 n
c=’X’;
21 0 n=15;
22 0 ptrn=&n;
23 15
return 0;
}
..
232 − 1
Sample program using pointers
ADDRESS OPERATIONS

There are two unary operations to consider.


The * operator: If ptr is a pointer variable, then ∗ptr
gives you the content of the location pointed to by ptr.
The & operator: If v is a variable, then &v is the address of
the variable.

*pointer
pointer data
& data
POINTER ARITHMETIC

Problem: How do we do relative addressing? (for example, “next


element” in an integer array)
C allows you to perform some arithmetic operations on pointers.
(Not every operation is allowed.) Consider
<datatype> *ptr; //datatype can be int, float, etc.
Unary Pointer Arithmetic Operators
Operator ++: Adds sizeof(datatype) number of bytes to
pointer, so that it points to the next entry of the datatype.
Operator −−: Subtracts sizeof(datatype) number of bytes to
pointer, so that it points to the next entry of the datatype.
POINTER ARITHMETIC - EXAMPLE 1

#include <stdio.h>

int main()
{
int *ptr; //increments by sizeof(int) (4 bytes)
long *ptrlng; //increments by sizeof(long) (8 bytes)

ptr++;
ptrlng++;

return 0;
}
POINTER ARITHMETIC - II

Pointers and integers are not interchangeable. (except for 0.) We will
have to treat arithmetic between a pointer and an integer, and
arithmetic between two pointers, separately.
Suppose you have a pointer to a long.
long *ptrlng;
Binary Operations between a pointer and an integer
1 ptrlng+n is valid, if n is an integer. The result is the following
byte address
ptrlng + n*sizeof(long)
and not ptrlng + n.
It advances the pointer by n number of longs.
2 ptrlng-n is similar.
Example of pointer arithmetic
POINTER ARITHMETIC - III
Consider two pointers ptr1 and ptr2 which point to the same type of
data.
<datatype> *ptr1, *ptr2;

Binary operations between two Pointers


1 Surprise: Adding two pointers together is not allowed!
2 ptr1 - ptr 2 is allowed, as long as they are pointing to elements
of the same array. The result is
ptr1 - ptr2
sizeof(datatype)

In other settings, this operation is undefined (may or may not give


the correct answer).
Example of pointer subtraction to find the
length of the array
POINTER ARITHMETIC - IV

If we can subtract a pointer from another, all the relational


operations can be supported!
Logical Operations on Pointers
1 ptr1 > ptr2 is the same as ptr1 - ptr2 > 0, ptr1 =
2 ptr2 is the same as ptr1 - ptr2 = 0, ptr1 < ptr2 is
3 the same as ptr1 - ptr2 < 0, and so on.
4
ARRAYS AND POINTERS

Array names essentially are pointers. Array elements are stored in


contiguous (consecutive) locations in memory.
For example, consider int arr[10];
1
arr is a pointer to the first element of the array.
2
That is, *arr is the same as arr[0].
ARRAYS AND
POINTERS

Array names essentially are pointers. Array elements are stored in


contiguous (consecutive) locations in memory.
For example, consider int arr[10];

1 arr is a pointer to the first element of the array. That is, *arr
2 is the same as arr[0].
3 arr+i is a pointer to arr[i]. (arr+iis equivalent to
arr+i*sizeof(int).)
ARRAYS AND POINTERS
- FIGURE . . . .
40 arr[0] = *arr = *(arr+0)
41

42

43

44 arr[1] = *(arr+1)
45

46

47

48 arr[2] = *(arr+2)
49

50

51

int arr[3]; .. ..
Printing array elements using pointers
Passing Pointers to Functions

Since pointers are also variables, they can be passed

As input parameters to functions


As return values from functions
PASSING POINTERS - REASON 1

Why do we pass pointer variables to functions?


Recall the swap function which took input integers. This function
was unable to swap the variables inside main().
Suppose we want a swap function which is able to swap arguments
inside the caller.
Main idea: Pass pointers!!
A SWAP PROGRAM USING CALL BY
REFERENCE
SCANF AND PRINTF

If we want to modify data in the caller, then we pass address of the


variables. We can see this in the difference between printf and
scanf.
scanf
scanf(‘‘ d’’, &n);
scanf needs to change the content of n. This can be done by
passing the address of n.

printf
printf(‘‘ d’’,n);
printf does not need to change the content of n.
Passing arrays to functions

We have already seen that we can pass arrays as input to functions.


We also have seen that arrays are essentially pointers.
We can pass pointers, where arrays are expected, and vice versa!
Passing Pointers to Functions - Another Reason

Passing a pointer to data, instead of passing the value of the data can
be much faster.
This is used to reduce the slowdown due to function calling.
The decision to do this must be taken with care.
ADVANTAGES OF POINTERS IN C

1. Pointers are useful for accessing memory locations.


2. Pointers provide an efficient way for accessing the elements of
an array structure.
3. Pointers are used for dynamic memory allocation as well as
deallocation.
4. Pointers are used to form complex data structures such as
linked list, graph, tree, etc.
DISADVANTAGES OF POINTERS IN C

1. Pointers are a little complex to understand.


2. Pointers can lead to various errors such as segmentation faults or can
access a memory location which is not required at all.
3. If an incorrect value is provided to a pointer, it may cause memory
corruption.
4. Pointers are also responsible for memory leakage.
5. Pointers are comparatively slower than that of the variables.
6. Programmers find it very difficult to work with the pointers; therefore it is
programmer's responsibility to manipulate a pointer carefully.

You might also like