IV Unit POINTERS
POINTERS
Address in C
Whenever a variable is defined in C language, a memory location is assigned for it, in which it's
value will be stored. We can easily check this memory address, using the & symbol.
If var is the name of the variable, then &var will give it's address.
Example:
#include<stdio.h>
void main()
{
int var = 7;
printf("Value of the variable var is: %dn", var);
printf("Memory address of the variable var is: %xn", &var);
}
What is pointer
The pointer is a variable which stores the address of another variable. This variable can be of
type int, char, array, function, or any other pointer.
Pointers are used to access memory and manipulate the address.
Features of Pointers
1. Pointers are more efficient in handling Arrays and Structures.
2. Pointers allow references to function and thereby helps in passing of function as arguments
to other functions.
3. It reduces length of the program and its execution time as well.
4. It allows C language to support Dynamic Memory management.
IV Unit POINTERS
Declaration of C Pointer variable
General syntax of pointer declaration is,
Datatype *pointer_name;
Int *p; or int *a; or int* b;
Data type of a pointer must be same as the data type of the variable to which the pointer variable
is pointing.
Initialization of C Pointer variable
Pointer Initialization is the process of assigning address of a variable to a pointer variable.
Pointer variable can only contain address of a variable of the same data type. In C
language address operator & is used to determine the address of a variable.
Example
#include<stdio.h>
void main()
{
int a = 10;
int *ptr; //pointer declaration
ptr = &a; //pointer initialization
}
Points to remember while using pointers
1. While declaring/initializing the pointer variable, * indicates that the variable is a pointer.
2. The address of any variable is given by preceding the variable name with Ampersand &.
3. The pointer variable stores the address of a variable. The declaration int *a doesn't mean
that a is going to contain an integer value. It means that a is going to contain the address of
a variable storing integer value.
4. To access the value of a certain address stored by a pointer variable, * is used. Here,
the * can be read as 'value at'.
IV Unit POINTERS
ARITHMETIC OPERATIONS WITH POINTERS
Pointer is a variable that points to a memory location. Memory addresses are numeric value that
ranges from zero to maximum memory size in bytes. These addresses can be manipulated like
simple variables. You can increment, decrement, calculate or compare these addresses manually.
C language provides a set of operators to perform arithmetic of memory addresses. Pointer
arithmetic in C is supported by following operators.
• Increment and decrement ++ and --
• Addition and Subtraction + and -
Incrementing Pointer (++) in C
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.
We can traverse an array by using the increment operation on a pointer which will keep pointing
to every element of the array, perform some operation on that, and update itself in a loop.
32-bit
For 32-bit int variable, it will be incremented by 2 bytes.
64-bit
For 64-bit int variable, it will be incremented by 4 bytes.
Example 1:
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p);
p=p+1;
printf("After increment: Address of p variable is %u n",p); // in our case, p will get incremented
by 4 bytes.
return 0;
}
IV Unit POINTERS
Output
Address of p variable is 3214864300
After increment: Address of p variable is 3214864304
Example 2:
#include<stdio.h>
void main ()
{
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
int i;
printf("printing array elements...n");
for(i = 0; i< 5; i++)
{
printf("%d ",*(p+i));
}
}
Decrementing Pointer (---) in C
Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start
pointing to the previous location.
32-bit
For 32-bit int variable, it will be decremented by 2 bytes.
64-bit
For 64-bit int variable, it will be decremented by 4 bytes.
Example:
#include <stdio.h>
void main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
IV Unit POINTERS
printf("Address of p variable is %u n",p);
p=p-1;
printf("After decrement: Address of p variable is %u n",p); // P will now point to the immidi
ate previous location.
}
Output:
Address of p variable is 3214864300
After decrement: Address of p variable is 3214864296
C Pointer Addition
We can add a value to the pointer variable.
For 64-bit int variable, it will add 4 * number.
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p);
p=p+3; //adding 3 to pointer variable
printf("After adding 3: Address of p variable is %u n",p);
return 0;
}
Output
Address of p variable is 3214864300
After adding 3: Address of p variable is 3214864312
As you can see, the address of p is 3214864300. But after adding 3 with p variable, it is
3214864312, i.e., 4*3=12 increment. Since we are using 64-bit architecture, it increments 12.
C Pointer Subtraction:
Like pointer addition, we can subtract a value from the pointer variable. Subtracting any number
from a pointer will give an address.
IV Unit POINTERS
For 64-bit int variable, it will subtract 4 * number.
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u n",p);
p=p-3; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u n",p);
return 0;
}
OUTPUT
Address of p variable is 3214864300
After subtracting 3: Address of p variable is 3214864288
You can see after subtracting 3 from the pointer variable, it is 12 (4*3) less than the previous
address value.

Pointers

  • 1.
    IV Unit POINTERS POINTERS Addressin C Whenever a variable is defined in C language, a memory location is assigned for it, in which it's value will be stored. We can easily check this memory address, using the & symbol. If var is the name of the variable, then &var will give it's address. Example: #include<stdio.h> void main() { int var = 7; printf("Value of the variable var is: %dn", var); printf("Memory address of the variable var is: %xn", &var); } What is pointer The pointer is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. Pointers are used to access memory and manipulate the address. Features of Pointers 1. Pointers are more efficient in handling Arrays and Structures. 2. Pointers allow references to function and thereby helps in passing of function as arguments to other functions. 3. It reduces length of the program and its execution time as well. 4. It allows C language to support Dynamic Memory management.
  • 2.
    IV Unit POINTERS Declarationof C Pointer variable General syntax of pointer declaration is, Datatype *pointer_name; Int *p; or int *a; or int* b; Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Initialization of C Pointer variable Pointer Initialization is the process of assigning address of a variable to a pointer variable. Pointer variable can only contain address of a variable of the same data type. In C language address operator & is used to determine the address of a variable. Example #include<stdio.h> void main() { int a = 10; int *ptr; //pointer declaration ptr = &a; //pointer initialization } Points to remember while using pointers 1. While declaring/initializing the pointer variable, * indicates that the variable is a pointer. 2. The address of any variable is given by preceding the variable name with Ampersand &. 3. The pointer variable stores the address of a variable. The declaration int *a doesn't mean that a is going to contain an integer value. It means that a is going to contain the address of a variable storing integer value. 4. To access the value of a certain address stored by a pointer variable, * is used. Here, the * can be read as 'value at'.
  • 3.
    IV Unit POINTERS ARITHMETICOPERATIONS WITH POINTERS Pointer is a variable that points to a memory location. Memory addresses are numeric value that ranges from zero to maximum memory size in bytes. These addresses can be manipulated like simple variables. You can increment, decrement, calculate or compare these addresses manually. C language provides a set of operators to perform arithmetic of memory addresses. Pointer arithmetic in C is supported by following operators. • Increment and decrement ++ and -- • Addition and Subtraction + and - Incrementing Pointer (++) in C 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. We can traverse an array by using the increment operation on a pointer which will keep pointing to every element of the array, perform some operation on that, and update itself in a loop. 32-bit For 32-bit int variable, it will be incremented by 2 bytes. 64-bit For 64-bit int variable, it will be incremented by 4 bytes. Example 1: #include<stdio.h> int main(){ int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p); p=p+1; printf("After increment: Address of p variable is %u n",p); // in our case, p will get incremented by 4 bytes. return 0; }
  • 4.
    IV Unit POINTERS Output Addressof p variable is 3214864300 After increment: Address of p variable is 3214864304 Example 2: #include<stdio.h> void main () { int arr[5] = {1, 2, 3, 4, 5}; int *p = arr; int i; printf("printing array elements...n"); for(i = 0; i< 5; i++) { printf("%d ",*(p+i)); } } Decrementing Pointer (---) in C Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start pointing to the previous location. 32-bit For 32-bit int variable, it will be decremented by 2 bytes. 64-bit For 64-bit int variable, it will be decremented by 4 bytes. Example: #include <stdio.h> void main(){ int number=50; int *p;//pointer to int p=&number;//stores the address of number variable
  • 5.
    IV Unit POINTERS printf("Addressof p variable is %u n",p); p=p-1; printf("After decrement: Address of p variable is %u n",p); // P will now point to the immidi ate previous location. } Output: Address of p variable is 3214864300 After decrement: Address of p variable is 3214864296 C Pointer Addition We can add a value to the pointer variable. For 64-bit int variable, it will add 4 * number. #include<stdio.h> int main(){ int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p); p=p+3; //adding 3 to pointer variable printf("After adding 3: Address of p variable is %u n",p); return 0; } Output Address of p variable is 3214864300 After adding 3: Address of p variable is 3214864312 As you can see, the address of p is 3214864300. But after adding 3 with p variable, it is 3214864312, i.e., 4*3=12 increment. Since we are using 64-bit architecture, it increments 12. C Pointer Subtraction: Like pointer addition, we can subtract a value from the pointer variable. Subtracting any number from a pointer will give an address.
  • 6.
    IV Unit POINTERS For64-bit int variable, it will subtract 4 * number. #include<stdio.h> int main(){ int number=50; int *p;//pointer to int p=&number;//stores the address of number variable printf("Address of p variable is %u n",p); p=p-3; //subtracting 3 from pointer variable printf("After subtracting 3: Address of p variable is %u n",p); return 0; } OUTPUT Address of p variable is 3214864300 After subtracting 3: Address of p variable is 3214864288 You can see after subtracting 3 from the pointer variable, it is 12 (4*3) less than the previous address value.