Updated March 16, 2023
Introduction to Pointers in C
The following article provides an outline for Pointers in C. In C or other programming languages, we have a concept of variable. These variables are used to hold values in them. One can use such variables while programming. However, there are few tasks that do not require variables but requires the address of those variables. The address of variables means the memory location actual where these variables are existing. We can define a pointer as a type of variable that holds the address of any other variable that might be holding some value.
Uses of Pointers in Programming Languages and C:
- Many tasks like dynamic memory allocation require pointers while programming in C.
- Using pointers, such a task could be done easily.
Different Ways of Accessing Variable Address in C
Let us try to know what are the different ways by which we can print the address of a variable in C.
There are two ways by which we can actually print the address of a variable.
Those two methods are:
- By using ampersand ( & ) method
- By using pointer
1. By using an ampersand (&) method
In this method, we will be using the concept of the ampersand to print the address of a variable.
Let us look this with an example.
Code:
#include <stdio.h>
int main () {
double varNumValue= 10.2;
char varTextValue[10] = "abc";
printf("The address of variable varNumValue: %x\n", &varNumValue );
printf("varTextValue variable address is : %x\n", &varTextValue );
return 0;
}
Output:
Although, one might see the different value which is assigned randomly while running the example.
The above-mentioned example is not much used, but it is worth knowing.
2. By using pointer
This is a modern approach to access the address of a variable.
Before using a pointer, let us know the general syntax of declaring a pointer.
The general syntax of declaring a pointer.
data_type *variable
This is a common way to declare any pointer in C, here data_type represents the type of the variable whose address needs to be stored. * denotes that the variable declared is a pointer. A variable is simply used for accessing the value.
A simple example of this is:
Code:
double *var_text
To use a pointer in C, basically, one needs to follow the following three steps:
- Defining of pointer variable.
- Assigning the address of the variable whose address we want to hold in the pointer variable.
- Now, once we have a pointer variable with the address, we can again retrieve the value of the variable from the address stored in the pointer. This could be done using * in C. * is simply one of the unary operators.
Let us use the above-mentioned steps with an example, and then we will see this example step by step.
Example:
Code:
#include <stdio.h>
int main () {
int varNumValue = 10;
int *ipointervarNumValue;
ipointervarNumValue = &varNumValue;
printf("Address of the variable varNumValue is: %x\n", &varNumValue );
printf("Address stored in the variable ipointervarNumValue is: %x\n", ipointervarNumValue);
printf("Value of the variable *ipointervarNumValue is: %d\n", *ipointervarNumValue );
return 0;
}
Output:
Now, let us try to understand the above example.
The line int varNumValue = 10; simply declares a variable with value 10.
The second line that is a line next to it: int * ; is simply declared to store the address of the variable.
The next code snippet, which is: ipointervarNumValue = &varNumValue, is used to hold the address of the variable.
Now, we can simply get the value and use a pointer in our code snippet.
Types of Pointer in C
There are different types of pointers in C:
- Null Pointer: A null pointer is a type of pointer which points to nothing. It generally points to the base address of the segment. In case of nothing is assigned to the pointer then it has a null value. It is generally used in header files like stdio.h, alloc.h.
- Dangler Pointer: This type of pointer can be created by deleting the object without modifying the pointer value.
- Generic Pointer: This type of pointer is declared using a variable of type void. Since it is of void data type, hence it is referred to as a generic pointer. This type of pointer does not point to any data.
- Wild Pointer: A pointer which has not been initialized is known as a wild pointer. This type of pointer is initialized during run time when one needs to use it. This type of pointer is known as wild pointer.
Conclusion – Pointers in C
Pointers in C are used to point to the address of the variable. These variables are used for the dynamic allocation of memory in C. These variables are declared with an asterisk so as to show that the variable is a pointer. These are used in the header file in programming.
Recommended Articles
This is a guide to Pointers in C. Here we discuss the different types and uses of pointers in C with methods and examples. You may also have a look at the following articles to learn more –