Unit IV Prog in C
Unit IV Prog in C
Advance Programming in C
Pointer:
The pointers perform the function of storing the addresses of other variables in the
program.
These variables could be of any type- char, int, function, array, or other pointers.
The pointer sizes depend on their architecture. (For ex. the size of a pointer in the 32-bit
architecture is 2 bytes.)
Let us look at an example where we define a pointer storing an integer’s address in a
program.
int x = 10;
int* p = &x;
Here, the variable p is of pointer type, and it is pointing towards the address of the x
variable, which is of the integer type.
2
Declaration of a Pointer
Just like the variables, we also have to declare the pointers in C before we use them in
any program. We can name these pointers anything that we like, as long as they abide
by the naming rules of the C language. Normally, the declaration of a pointer would take
a form like this: data_type * name_of_pointer_variable;
● The data_type refers to this pointer’s base type in the variable of C. It indicates which type of
variable is the pointer pointing to in the code.
● The asterisk ( * ) is used to declare a pointer. It is an indirection operator, and it is the same
asterisk that we use in multiplication.
We can declare pointers in the C language with the use of the asterisk symbol ( * ). It is
also called the indirection pointer, as we use it for dereferencing any pointer. Here is
how we use it:
We use the & (ampersand) operator to get the variable’s address in the program. We
place the & just before that variable’s name whose address we require. Here is the
syntax that we use for the initialisation of a pointer,
pointer = &variable;
3
As we can assess in the figure shown above, the pointer variable is storing the digit
variable’s address, named fff4. This digit variable’s value is equal to 100. But aaa3 is
the pointer variable’s address (variable r).
Here, we can print the r pointer variable’s value using the indirection pointer ( * ). We will
look at an example for the same, as explained in the figure mentioned above.
#include<stdio.h>
int main(){
int digit=100;
int *r;
r=&digit; // for storing the address of the digit variable in the program
printf(“The address of the variable r is equal to %x \n”,r); // pointer r contains the digit
variable’s address, and thus, printing the variable r gives us the address of the digit.
return 0;
}
4
Use of Pointers in C
Here is a summary of how we use the following operators for the pointers in any
program:
int *q [20] = &arr; // The q variable of the pointer type is pointing towards the integer
array’s address or the address of arr.
void(*q)(int) = &show; // The q pointer is pointing towards the function’s address in the
program
struct str {
int x;
float y;
}ref;
void apoo()
int* ptr;
ptr = &var;
// Driver program
int main()
apoo();
return 0;
Types of Pointers
7
There are various types of pointers that we can use in the C language. Let us take a
look at the most important ones.
#include <stdio.h>
int main()
return 0;
The output obtained out of the program mentioned above will be:
#include <stdio.h>
int main()
return 0;
The output obtained out of the program mentioned above will be:
#include <stdio.h>
int main()
printf(“\n%d”,*w);
9
return 0;
The output obtained out of the program mentioned above will be a compile-time error.
Here are a few more types of pointers that you can find in the C programs:
● Near pointer
● Complex pointer
● Huge pointer
● Far pointer
● Dangling pointer
● Direct Access: In this case, we use the variable name in the program directly.
● Indirect Access: In this case, we use some pointers to the variables in the program.
Let us take a look at a program to understand this better. Consider the below program:
#include <stdio.h>
int variable = 1;
int *ptr;
{
10
ptr = &variable;
/*changing of the content of the variable through the ptr pointer in the program*/
*ptr=48;
return 0;
The compilation of this program devoid of errors would generate an output like this:
Cons of Pointers in C
● The concept of pointers is a bit tricky to understand.
● These can lead to some errors like segmentation faults, accessing of unrequired memory
locations, and many more.
● A pointer may cause corruption of memory if we provide it with an incorrect value.
● Pointers in a program can lead to leakage of memory.
● As compared to all the other variables, pointers are somewhat slower.
● Some programmers might find it very tricky to deal with pointers in a program. It is their
responsibility to use these pointers and manipulate them carefully.
Applications of Pointers in C
There are various uses of the pointers in C language. Let us look at some of them:
2. Structures, Functions, and Arrays: We generally use the pointers in the C language in
the cases of structures, functions, and arrays. Here, the pointers help in reducing the
code and improving the program’s performance.
12
#include<stdio.h>
int main(){
int digit=100;
printf(“The value of the digit is equal to %d and the address of the digit is equal to
%u”,digit,&digit);
return 0;
The value of the digit is equal to 100 and the address of the digit is equal to fff4
Pointer Arithmetics in C:
Pointer Arithmetic is the set of valid arithmetic operations that can be performed
on pointers.
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
13
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.
For Example:
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.
14
Example:
#include <stdio.h>
int main()
int a = 22;
int *p = &a;
p++;
p--;
float b = 22.22;
float *q = &b;
q++;
q--;
char c = 'a';
15
char *r = &c;
r++;
r--;
return 0;
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.
16
#include <stdio.h>
// Driver Code
int main()
{
// Integer variable
int N = 4;
// Pointer to an integer
int *ptr1, *ptr2;
// Addition of 3 to ptr2
ptr2 = ptr2 + 3;
17
return 0;
}
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.
18
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.
#include <stdio.h>
int main()
int N = 4;
// Pointer declaration
x = ptr2 - ptr1;
x);
return 0;
5. Comparison of Pointers
20
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.
#include <stdio.h>
int main()
int arr[5];
if (ptr1 == ptr2) {
"are Equal.");
}
21
else {
return 0;
#include <stdio.h>
a = b;
b = temp;
}
// Driver code
int main()
{
int a = 10, b = 20;
swap(a, b);
printf("Values after swap function are: %d, %d",
a, b);
return 0;
}
utput
Values after swap function are: 10, 20
C
23
// swap function.
#include <stdio.h>
int temp;
24
temp = *a;
*a = *b;
*b = temp;
// Driver code
int main()
{
25
a, b);
swap(&a, &b);
a, b);
return 0;
Output
C – Pointer to Pointer:
The pointer to a pointer in C is used when we want to store the address of
another pointer. The first pointer is used to store the address of the variable.
And the second pointer is used to store the address of the first pointer. That is
why they are also known as double-pointers. We can use a pointer to a pointer
to change the values of normal pointers or create a variable-sized 2-D array. A
double pointer occupies the same amount of space in the memory stack as a
normal pointer.
int val = 5;
The first pointer ptr1 stores the address of the variable and the second pointer
ptr2 stores the address of the first pointer.
#include <stdio.h>
int main()
int* ptr2;
int** ptr1;
ptr2 = &var;
ptr1 = &ptr2;
return 0;
● They can be used to store multilevel data such as the text document
paragraph, sentences, and word semantics.
● They are used in data structures to directly manipulate the address of
the nodes without copying.
● They can be used as function arguments to manipulate the address
stored in the local pointer.