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

C Pointers

Uploaded by

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

C Pointers

Uploaded by

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

C Pointers

Pointers are one of the core components of the C programming language. A pointer
can be used to store the memory address of other variables, functions, or even
other pointers. The use of pointers allows low-level memory access, dynamic
memory allocation, and many other functionality in C.

What is a Pointer in C?
A pointer is defined as a derived data type that can store the address of other C
variables or a memory location. We can access and manipulate the data stored in
that memory location using pointers.

Syntax of C Pointers
The syntax of pointers is similar to the variable declaration in C, but we use the ( *
) dereferencing operator in the pointer declaration.
datatype * ptr;
where
• ptr is the name of the pointer.
• datatype is the type of data it is pointing to.
The above syntax is used to define a pointer to a variable. We can also define
pointers to functions, structures, etc.

How to Use Pointers?


The use of pointers in C can be divided into three steps:
1. Pointer Declaration
2. Pointer Initialization
3. Pointer Dereferencing
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To
declare a pointer, we use the ( * ) dereference operator before its name.
Example
int *ptr;
The pointer declared here will point to some random memory address as it is not
initialized. Such pointers are called wild pointers.
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer
variable. We generally use the ( &: ampersand ) addressof operator to get the
memory address of a variable and then store it in the pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;
We can also declare and initialize the pointer in a single step. This method is
called pointer definition as the pointer is declared and initialized at the same time.
Example
int *ptr = &var;
The pointer declared here will point to some random memory address as it is not
initialized. Such pointers are called wild pointers.
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer
variable. We generally use the ( &: ampersand ) addressof operator to get the
memory address of a variable and then store it in the pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;
We can also declare and initialize the pointer in a single step. This method is
called pointer definition as the pointer is declared and initialized at the same time.
Example
int *ptr = &var;
Note: It is recommended that the pointers should always be initialized to some
value before starting using it. Otherwise, it may lead to number of errors.
3. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the memory
address specified in the pointer. We use the same ( * ) dereferencing operator that
we used in the pointer declaration.

Dereferencing a Pointer in C

C Pointer Arithmetic
Pointer Arithmetic is the set of valid arithmetic operations that can be performed
on pointers. The pointer variables store the memory address of another variable. It
doesn’t store any value.
Hence, there are only a few operations that are allowed to perform on Pointers in
C language. The C pointer arithmetic operations are slightly different from the ones
that we generally use for mathematical calculations. These operations are:
1. Increment/Decrement of a Pointer
2. Addition of integer to a pointer
3. Subtraction of integer to a pointer
4. Subtracting two pointers of the same type
5. Comparison of pointers

1. Increment/Decrement of a Pointer
Increment: It is a condition that also comes under addition. When a pointer is
incremented, it actually increments by the number equal to the size of the data
type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is incremented, then it will
increment by 4(size of an int), and the new address will point to 1004. While if a
float type pointer is incremented then it will increment by 4(size of a float) and
the new address will be 1004.
Decrement: It is a condition that also comes under subtraction. When a pointer is
decremented, it actually decrements by the number equal to the size of the data
type for which it is a pointer.
ForExample:
If an integer pointer that stores address 1000 is decremented, then it will
decrement by 4(size of an int), and the new address will point to 996. While if a
float type pointer is decremented then it will decrement by 4(size of a float) and
the new address will be 996.

Note: It is assumed here that the architecture is 64-bit and all the data types are
sized accordingly. For example, integer is of 4 bytes.

2. Addition of Integer to Pointer


When a pointer is added with an integer value, the value is first multiplied by the
size of the data type and then added to the pointer.
For Example:
Consider the same example as above where the ptr is an integer pointer that
stores 1000 as an address. If we add integer 5 to it using the expression, ptr = ptr +
5, then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) * 5 =
1020.
3. Subtraction of Integer to Pointer
When a pointer is subtracted with an integer value, the value is first multiplied by
the size of the data type and then subtracted from the pointer similar to addition.
For Example:
Consider the same example as above where the ptr is an integer pointer that
stores 1000 as an address. If we subtract integer 5 from it using the
expression, ptr = ptr – 5, then, the final address stored in the ptr will be ptr = 1000
– sizeof(int) * 5 = 980.

4. Subtraction of Two Pointers


The subtraction of two pointers is possible only when they have the same data type.
The result is generated by calculating the difference between the addresses of the
two pointers and calculating how many bits of data it is according to the pointer
data type. The subtraction of two pointers gives the increments between the two
pointers.
For Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are
subtracted. The difference between addresses is 4 bytes. Since the size of int is 4
bytes, therefore the increment between ptr1 and ptr2 is given by (4/4) = 1.

5. Comparison of Pointers
We can compare the two pointers by using the comparison operators in C. We can
implement this by using all operators in C >, >=, <, <=, ==, !=. It returns true for the
valid condition and returns false for the unsatisfied condition.
1. Step 1: Initialize the integer values and point these integer values to the
pointer.
2. Step 2: Now, check the condition by using comparison or relational
operators on pointer variables.
3. Step 3: Display the output.

C Pointers and Arrays


In C programming language, pointers and arrays are closely related. An array name
acts like a pointer constant. The value of this pointer constant is the address of the
first element. For example, if we have an array named val then val and &val[0] can
be used interchangeably.
If we assign this value to a non-constant pointer of the same type, then we can
access the elements of the array using this pointer.
Example 1: Accessing Array Elements using Point er with Array Subscript

The following figure shows the pointer p and ptr. The darker arrow denotes a
pointer to an array.

On dereferencing a pointer expression we get a value pointed to by that pointer


expression. The pointer to an array points to an array, so on dereferencing it, we
should get the array, and the name of the array denotes the base address. So
whenever a pointer to an array is dereferenced, we get the base address of the
array to which it points.

Pointer to Multidimensional Arrays


1. Pointers and Two-Dimensional Arrays
In a two-dimensional array, we can access each element by using two subscripts,
where the first subscript represents the row number and the second subscript
represents the column number. The elements of 2-D array can be accessed with the
help of pointer notation also. Suppose arr is a 2-D array, we can access any
element arr[i][j] of the array using the pointer expression *(*(arr + i) + j). Now we’ll
see how this expression can be derived.
Let us take a two dimensional array arr[3][4]
int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };

Since memory in a computer is organized linearly it is not possible to store the 2-D
array in rows and columns. The concept of rows and columns is only theoretical,
actually, a 2-D array is stored in row-major order i.e rows are placed next to each
other. The following figure shows how the above 2-D array will be stored in memory.

Each row can be considered as a 1-D array, so a two-dimensional array can be


considered as a collection of one-dimensional arrays that are placed one after
another. In other words, we can say that 2-D dimensional arrays that are placed one
after another. So here arr is an array of 3 elements where each element is a 1-D
array of 4 integers.

We know that the name of an array is a constant pointer that points to 0th 1-D array
and contains address 5000. Since arr is a ‘pointer to an array of 4 integers’,
according to pointer arithmetic the expression arr + 1 will represent the address
5016 and expression arr + 2 will represent address 5032.
So we can say that arr points to the 0th 1-D array, arr + 1 points to the 1st 1-D array
and arr + 2 points to the 2nd 1-D array.
In general we can write:
arr + i Points to ith element of arr ->
Points to ith 1-D array
• Since arr + i points to ith element of arr, on dereferencing it will get
ith element of arr which is of course a 1-D array. Thus the expression *(arr +
i) gives us the base address of ith 1-D array.
• We know, the pointer expression *(arr + i) is equivalent to the subscript
expression arr[i]. So *(arr + i) which is same as arr[i] gives us the base
address of ith 1-D array.
• To access an individual element of our 2-D array, we should be able to
access any jth element of ith 1-D array.
• Since the base type of *(arr + i) is int and it contains the address of
0th element of ith 1-D array, we can get the addresses of subsequent
elements in the ith 1-D array by adding integer values to *(arr + i).
• For example *(arr + i) + 1 will represent the address of 1st element of
1stelement of ith 1-D array and *(arr+i)+2 will represent the address of
2nd element of ith 1-D array.
• Similarly *(arr + i) + j will represent the address of jth element of ith 1-D
array. On dereferencing this expression we can get the jth element of the
ith 1-D array.
Advantages of Pointers
Following are the major advantages of pointers in C:
• Pointers are used for dynamic memory allocation and deallocation.
• An Array or a structure can be accessed efficiently with pointers
• Pointers are useful for accessing memory locations.
• Pointers are used to form complex data structures such as linked lists,
graphs, trees, etc.
• Pointers reduce the length of the program and its execution time as well.

Disadvantages of Pointers
Pointers are vulnerable to errors and have following disadvantages:
• Memory corruption can occur if an incorrect value is provided to pointers.
• Pointers are a little bit complex to understand.
• Pointers are majorly responsible for memory leaks in C.
• Pointers are comparatively slower than variables in C.
• Uninitialized pointers might cause a segmentation fault.

You might also like