Pointers Unit 4th 2nd Sem
Pointers Unit 4th 2nd Sem
Unit 4
Pointers
What is Pointer in C?
➢ The Pointer is a variable that stores address of another variable.
➢ A pointer can also be used to refer to another pointer function.
➢ A pointer can be incremented/decremented, i.e., to point to the next/
previous memory location.
➢ The purpose of pointer is to save memory space and achieve faster
execution time.
Declaring a Pointer
A pointer declaration has the following form.
data_type * pointer_variable_name;
Here,
• data_type is the pointer's base type of C's variable types and indicates
the type of the variable that the pointer points to.
• The asterisk (*: the same asterisk used for multiplication) which is
indirection operator, declares a pointer.
Page 1 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
Initialize a pointer
After declaring a pointer, we initialize it like standard variables with a variable
address. If pointers in C programming are not uninitialized and used in the
program, the results are unpredictable and potentially disastrous.
Pointer Syntax
pointer = &variable;
#include <stdio.h>
int main()
{
int a=10; //variable declaration
int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
printf("Address stored in a variable p is:%x\n",p); //accessing the address
printf("Value stored in a variable p is:%d\n",*p); //accessing the value
return 0;
}
Output:
Operator Meaning
* Serves 2 purpose
1. Declaration of
a pointer
2. Returns the
value of the
referenced
variable
Page 2 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
• Returns the
address of a
variable
Features of Pointers:
Uses of pointers:
Page 3 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
o Increment
o Decrement
o Addition
o Subtraction
o Comparison
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.
32-bit
64-bit
For 64-bit int variable, it will be incremented by 4 bytes.
Page 4 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
1. #include<stdio.h>
2. int main(){
3. int number=50;
7. p=p+1;
9. return 0;
10. }
Output
1. #include<stdio.h>
2. void main ()
3. {
5. int *p = arr;
6. int i;
9. {
11. }
12. }
Page 5 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
Output
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. The formula of
decrementing the pointer is given below:
64-bit
For 64-bit int variable, it will be decremented by 4 bytes.
1. #include <stdio.h>
2. void main(){
3. int number=50;
7. p=p-1;
9. }
Output
Page 6 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
C Pointer Addition
We can add a value to the pointer variable. The formula of adding value
to pointer is given below:
64-bit
1. #include<stdio.h>
2. int main(){
3. int number=50;
9. return 0;
10. }
Output
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. But if we were using 32-bit
architecture, it was incrementing to 6 only, i.e., 2*3=6. As integer value
occupies 2-byte memory in 32-bit OS.
Page 7 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
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. The formula
of subtracting value from the pointer variable is given below:
64-bit
For 64-bit int variable, it will subtract 4 * number.
Let's see the example of subtracting value from the pointer variable on
64-bit architecture.
1. #include<stdio.h>
2. int main(){
3. int number=50;
9. return 0;
10. }
Output
You can see after subtracting 3 from the pointer variable, it is 12 (4*3)
less than the previous address value.
Page 8 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
pointer points
1. #include<stdio.h>
2. void main ()
3. {
4. int i = 100;
5. int *p = &i;
6. int *temp;
7. temp = p;
8. p = p + 3;
10. }
Output
Page 9 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali
Programming With C &C++
o ~Address = illegal
Page 10 of 10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.
Laxman Anukali