0% found this document useful (0 votes)
5 views24 pages

Pointers in C

Uploaded by

devaraj48212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views24 pages

Pointers in C

Uploaded by

devaraj48212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Pointers in C

C Pointer

• The pointer in C language is a variable which stores the address of another


variable. This variable can be of type int, char, array, function, or any other
pointer. The size of the pointer depends on the architecture. However, in 32-bit
architecture the size of a pointer is 4 byte and 16-bit architecture the size of a
pointer is 2 byte.
1. Ex: int n = 10;
2.int* p = &n;
// Variable p of type pointer is pointing to the address of the variable n of type intege
r.
Declaration

• The pointer in c language can be declared using * (asterisk symbol). It is also


known as indirection pointer used to dereference a pointer.
• Syntax : data_type * pname;
• Ex: int *p;
• As you can see in the above figure, pointer variable stores the address of number
variable, i.e., fff4. The value of number variable is 50. But the address of pointer
variable p is aaa3.
• By the help of * (indirection operator), we can print the value of pointer variable
p.
Ex :

#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number; //stores the address of number variable
printf("Address of p variable is %x \n",p); // p contains the address of the
number
printf("Value of p variable is %d \n",*p);
return 0;
}
Output:

• Address of p variable is fff4


• Value of p variable is 50
Advantages

1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.

• Address Of (&) Operator


• The address of operator '&' returns the address of a variable. But we need to use
%u to display the address of a variable in integer value and %x will display in the
type of hexa-decimal form.
Double pointer (Multiple indirection, pointer to pointer).

• As we know that, a pointer is used to store the address of a variable in C. Pointer


reduces the access time of a variable. However, In C, we can also define a pointer
to store the address of another pointer. Such pointer is known as a double pointer
(pointer to pointer). The first pointer is used to store the address of a variable
whereas the second pointer is used to store the address of the first pointer. Let's
understand it by the diagram given below.
Syntax:

• data_type **pname;

• Ex: int **p;


Ex:
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
printf("address of a: %x\n",p); // Address of a will be printed
printf("address of p: %x\n",pp); // Address of p will be printed
printf("value stored at p: %d\n",*p);
printf("value stored at p: %d\n",**p);
}
Output

• address of a: d268734
• address of a: d268734
• value stored at p: 10
• value stored at pp: 10
• Double pointer
Pointer Arithmetic

• We can perform arithmetic operations in pointer. Same like as in normal


operations. But the difference is we will perform in address while we performing
arithmetic operations on pointer we have to perform on address and output will be
in the integer format.

• Increment, decrement, addition, subtraction.


Incrementing pointer

• 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.

• Syntax: new_address= current_address + i * size_of(data type) ;


Ex:

#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; //p++
printf("After increment: Address of p variable is %u \n",p); // in our case, p will get i
ncremented by 4 bytes.
return 0;
}
• Output:

• Address of P variable is 1000


• After increment: Address of p variable is 1004
Pointer Decrement

• Like increment, we can decrement a pointer variable. If


we decrement a pointer, it will start pointing to the
previous location.

• Syntax: new_address= current_address - i * size_of(data type) ;


Ex:

#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; //p
printf("After decrement: Address of p variable is %u \n",p); // in our case, p will get
decremented by 4 bytes.
return 0;
}
• Output:

• Address of P variable is 1000


• After decrement: Address of p variable is 996
Pointer addition

• Same like in addition operator we will add address in the pointer.

• Syntax: new_address= current_address + (number * size_of(data type)) ;


Ex:

#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 1000


• After addition: Address of p variable is 1012
Pointer subtraction

• Same like addition it will subtract the address and store it another
address.
• Syntax: new_address= current_address - (number * size_of(data type));
Ex:

#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 to pointer variable
printf("After subtract 3: Address of p variable is %u \n",p);
return 0;
}
• Output:

• Address of P variable is 1000


• After subtract: Address of p variable is 988

You might also like