Table of Contents
Pointers in C programming
In simple terms pointers are the variables which store addresses of another variable.
It can also store addresses of functions, arrays, structures and other pointers along with primitive data type variables.
But the question arises “why do we need pointers?“.
Let’s find out the answer for the above question.
Declaration of pointers
Declaration of pointer variables is almost same as declaring normal variable, only difference is that we add ‘* (asterisk)‘ sign before the pointer variable.
int *num; // pointer variable which can store address of int data type variable.
char *a; // pointer variable which can store address of char data type variable.
Size of the pointer is dependent on the architecture.
Assigning value to pointer
Pointer accepts the address of another variable, so the first question we must ask is “how to get an address of any variable?”.
This is where “address unary operator” comes into the picture, ‘& (ampersand)’ is used to get the address of variables.
Now we can store this address in the pointer variable.
data_type *pointer_name = &another_variable;
int a = 8, b = 12;
int *ptr = &a;
ptr2 = &b;
Print value of pointer
To get an address stored in a pointer variable we have to write the name of the pointer variable as shown in below example.
To access the value which is stored in that particular address (which is stored in pointer variable) we have to use ‘* (asterisk)’ sign before the pointer variable as shown in the below example.
In the below example ptr is a pointer variable which stores the address of int variable x.
When we write “ptr” in a printf statement we are printing the value which is stored in ptr i.e. address of variable x we can print address in decimal and hexadecimal format as well.
When we write “*ptr” in printf statement we are printing the actual value which is stored on that particular address (which is stored in pointer variable) i.e. 10 which is the value of variable x.
int x = 10;
ptr = &x; //address of x is stored in ptr
// This will print the address in decimal format which is stored in ptr
printf("nptr = %d",ptr);
// This will print the address in hexadecimal format which is stored in ptr
printf("nptr = %x",ptr);
// This will print the value which is stored on that address which stored in ptr
printf("n*ptr = %d",*ptr);
ptr = 6422312
ptr = 61ff28
*ptr = 10
What is the NULL pointer?
If we don’t have to assign any value to the pointer at the time of declaration then we can assign “NULL” value to the pointer.
Although it is not compulsory or there is no rule to do this but it is a convenient or good practice to do so.
int *ptr = NULL;
If we print the value of the above pointer then we will get 0 in output.
Most OS do not allow to access address 0 because it is reserved for OS.
Bus address 0 has special significance because it states that the pointer is not pointing towards accessible memory location.
Swap two numbers using Pointers
void swap(int *a, int *b)
temp = *a;
*a = *b;
*b = temp;
int x, y;
printf("Enter first number : ");
printf("Enter second number : ");
printf("Value of X before swapping = %dnValue of Y before swapping = %d", x, y);
printf("nValue of X after swapping = %dnValue of Y after swapping = %d", x, y);
Enter first number : 5
Enter second number : 8
Value of X before swapping = 5
Value of Y before swapping = 8
Value of X after swapping = 8
Value of Y after swapping = 5
Advantages of pointers
- With the help of pointers we can return multiple values from the function.
- Pointers help to reduce the code.
- We can access the memory location using pointers.
Connect with us on following platforms
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
|Quizzes on Operator|
|Easy Quiz on Operators in C part 1|
|Easy quiz on operators in C part 2|
|Easy quiz on operators in C part 3|
|Quizzes on Loops|
|Easy quiz on loops in C part 1|
|Easy quiz on loops in C part 2|
|Easy quiz on loops in C part 3|
|Quizzes on Decision Making Statement|
|Easy Quiz On Decision Making Statements Part 1|
|Easy Quiz On Decision Making Statements Part 2|
|Quizzes on String|
|Easy quiz on String in C programming part 1|
|Quizzes on Array|
|Easy quiz on Array in C programming – part 1|
|5 – Best Java program to convert decimal to binary|
|New Quizzes Coming soon..|