In this article, we are going to learn about pointers. We will learn How to Create Initialize and use pointers in C, How to initialize pointers, and also how to use pointers in C programs. We will learn each concept with the help of code examples.
Pointers are those variables that are used to access the memory locations. Using pointers we can get access to the exact memory address where a variable is pointing to. Pointers works with mainly two operators which are & Operator and the * operator. & operators help us get to the address of that variable in memory and * operator helps us get to the value that is saved at that memory location.
Creation of Pointer Variables in C
The syntax that is used to declare a pointer variable in C language is:
<data-type> * <variable-name>
Example of the pointer declaration
int * iptr;
float * fptr
char * cPtr;
long * lptr;
Types of Pointer in C
- NULL Pointer – A null pointer is a type of pointer which does not points to any memory.
- Generic Pointers – A pointer that is declared as void data type.It means it is not bound to any specific data type.
- Dangling Pointer – A pointer that is created by deleting an object pointer but not initialized to proper value.
- Wild Pointer – A pointer that is declared but not initialized.
C program to create, initialize and use pointers
In this example, we are going to declare pointers and then initialize values to pointers and access those values using the * operator. We will also learn the use of & operator in C pointers. So let us begin with our code example.
#include <stdio.h>
int main()
{
// Declaring an Integer variable.
int var = 12;
// Declaring an integer pointer variable
int * iptr;
// Assigning the integer variable's address to pointer.
iptr = &var;
printf("The memory address of var is = %x\n", &var);
printf("The Value in var is = %d\n", var);
printf("The memory address of iptr is = %x\n", &iptr);
printf("The memory address Pointed by iptr is = %x\n", iptr);
printf("The value saved at iptr address is = %d\n", *iptr);
return 0;
}
Output
The memory address of var is = 6cd904fc
The Value in var is = 12
The memory address of iptr is = 6cd90500
The memory address Pointed by iptr is = 6cd904fc
The value saved at iptr address is = 12
C program to get memory address of Character using address of operator
In this example, we are trying to learn about the character data type and how we can use a pointer to get its address in memory and value saved at that address
#include <stdio.h>
int main()
{
// Declaring an Integer variable.
char var = 'D';
// Declaring an integer pointer variable
int * iptr;
// Assigning the integer variable's address to pointer.
iptr = &var;
printf("The memory address of var is = %x\n", &var);
printf("The Value in var is = %c\n", var);
printf("The memory address of iptr is = %x\n", &iptr);
printf("The memory address Pointed by iptr is = %x\n", iptr);
printf("The value saved at iptr address is = %c\n", *iptr);
return 0;
}
Output
The memory address of var is = da8b1e4f
The Value in var is = D
The memory address of iptr is = da8b1e50
The memory address Pointed by iptr is = da8b1e4f
The value saved at iptr address is = D