Pointers In C – Answer From [A-Z]

A variable that stores the address of another variable is known as a pointer. In contrast to other variables that hold values of a specific type, a pointer variable holds the address of a variable. An integer variable, for example, holds (or stores) an integer value, whereas an integer pointer holds the address of an integer variable. With the help of examples, we will go over pointers in C programming.
Below we will dive into the pointers in C, let’s look at a simple example to see what we mean by a variable’s address.

In C, what is a pointer?

In C, a pointer is a variable that stores the address of another variable. A pointer function can also refer to another pointer function. A pointer can be incremented or decremented, indicating the next or previous memory location. The purpose of a pointer is to save memory space and speed up execution.

Definition, notation, classification, and arithmetic

1. What are pointers exactly?

Before we get into the definition of pointers, consider what happens when the following code is executed:

int digit = 42;

The compiler reserves a block of memory to hold an int value. This block’s name is digit, and the value stored in it is 42.
To help you remember the block, it has been assigned an address or a location number (say, 24650).
The value of the location number is unimportant to us because it is generated at random. However, we can get to this address by using the & (ampersand) or the operator’s address.

printf("The address of digit = %d.",&digit);
 /* prints "The address of digit = 24650. */

We can get the variable digit’s value from its address by using another operator * (asterisk), known as the indirection, dereferencing, or value at address operator.

printf("The value of digit = %d.", *(&digit);
 /* prints "The value of digit = 42. */

2. Notation and Definition

A variable’s address can be stored in another variable known as a pointer variable. The following is the syntax for storing a variable’s address to a pointer:

dataType *pointerVariableName = &variableName;

This can be written for our digit variable as follows:

int *addressOfDigit = &digit;

or like this:

int *addressOfDigit;
addressOfDigit= &digit;

This can be read as – A pointer to int (integer) addressOfDigit stores the address of the variable(&) digit.
A few points to consider:
dataType – We must inform the computer of the data type of the variable whose address we are storing. In this case, the data type of digit was int.
This does not imply that addressOfDigit will store an int value. An integer pointer (like addressOfDigit) can only store the address of integer-type variables.

int variable1;
int variable2;
char variable3;
int *addressOfVariables;

Making a Pointer

Pointers, like variables, must be declared in C programming before they can be used in your program. Pointers can be named whatever you want as long as they follow C’s naming conventions. The following is the syntax for a pointer declaration.

data_type * pointer_variable_name;

In this case, data type is the pointer’s base type of C’s variable types, indicating the type of variable to which the pointer points.
The indirection operator, the asterisk (*: the same asterisk used for multiplication), declares a pointer.
In this C pointers tutorial, we’ll look at some valid pointer declarations:

int    *ptr_thing;            /* pointer to an integer */ 
int *ptr1,thing;/* ptr1 is a pointer to type integer and thing is an integer variable */
double    *ptr2;    /* pointer to a double */
float    *ptr3;      /* pointer to a float */
char    *ch1 ;       /* pointer to a character */
float  *ptr, variable;/*ptr is a pointer to type float and variable is an ordinary float variable */

Pointer Types in C

The following are the various types of pointers in C:

The Null Pointer

By assigning a null value during the pointer declaration, we can create a null pointer. This method is useful when the pointer has no address assigned to it. A null pointer always has the value 0.
The following program shows how to use a null pointer:

#include <stdio.h>
int main()
	int *p = NULL; 	//null pointer
	printf(“The value inside variable p is:\n%x”,p);
	return 0;


The value inside variable p is:

Pointer Into the Void

A void pointer is also known as a generic pointer in C programming. There are no standard data types. The keyword void is used to create a void pointer. It can be used to store any variable’s address.
The following program shows how to use a void pointer:

#include <stdio.h>
int main()
void *p = NULL; 	//void pointer
printf("The size of pointer is:%d\n",sizeof(p));
return 0;


The size of pointer is:4

The erratic pointer

If a pointer is not initialized to anything, it is said to be a wild pointer. These types of C pointers are inefficient because they may point to an unknown memory location, causing problems in our program and possibly causing it to crash. Working with wild pointers requires extreme caution.
The following program demonstrates how to use a wild pointer:

#include <stdio.h>
int main()
int *p; 	//wild pointer
return 0;


timeout: the monitored command dumped core
sh: line 1: 95298 Segmentation fault      timeout 10s main


A variable whose value is the address of another variable of the same type is referred to as a pointer. The value of the variable to which the pointer is dereferencing with the * operator. There are several types of pointers: void, null, dangling, wild, near, far, and huge.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button