Pointers in C

Safalta Expert Published by: Saksham Chauhan Updated Thu, 23 Feb 2023 04:38 PM IST

Highlights

Check Pointers in C Here At Safalta.com

Source: Safalta.com

Learning pointers in C is simple and enjoyable. Pointers make some C programming tasks easier to do, but they are essential for completing others, including dynamic memory allocation.
Therefore, learning pointers is essential if you want to become a master C programmer. Let's begin to study them in easy and basic steps. Every variable is a memory location, and each memory location has a determined address that may be accessed using the ampersand (&) operator, which stands for a memory address. Take a look at the sample below, which displays the location of the declared variables. Download these FREE Ebooks:

1. Introduction to Digital Marketing
2. Website Planning and Creation

Pointers

A pointer is a variable whose value is the direct address of a memory region, or the address of another variable. A pointer must be declared before it may be used to hold any variable address, just like any variable or constant. A pointer variable declaration's general form is
type *var-name;

Here, var-name is the name of the pointer variable, and type is the base type of the pointer; it must be a legitimate C data type. The asterisk * that is used to denote a pointer is also used to denote multiplication. However, the asterisk in this sentence indicates that a variable is a pointer. Examine a few of the legal pointer declarations.

int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

All pointers, regardless of whether they are integer, float, character, or another type, really have the same value, which is a lengthy hexadecimal number that corresponds to a memory address. The data type of the variable or constant that the pointer links to is the only distinction between pointers of various data kinds.

How Do Pointers Work?

There are a few significant procedures that we will use pointers for rather frequently. A pointer variable is defined, a variable's address is assigned to a pointer, and then a value is accessed at the location specified in the pointer variable. Utilizing the unary operator *, which gives the value of the variable located at the address given by its argument, accomplishes this. These operations are used in the example that follows.

The outcome of compiling and running the aforementioned code is as follows:

Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20

C NULL pointer

At a high level, NULL may be viewed as a null pointer that is employed in C for a variety of functions. The following are some of the most typical uses for NULL: When a pointer variable hasn't yet been given a legitimate memory address, initialising it is necessary. b) Before accessing any pointer variable, look for a null pointer. By doing this, we may handle errors in pointer-related code, for example, by only dereference a pointer variable if it is not NULL. b) When we don't wish to give any valid memory addresses, we can pass a null pointer as an argument to a function.

Multiple standard libraries specify the constant known as the NULL pointer as having a value of zero. Think about the following programme:

The outcome of compiling and running the aforementioned code is as follows:

The value of ptr is 0

Because the operating system has reserved memory at address 0, most operating systems do not allow applications to access that memory. The memory address 0 has a specific meaning, however, since it indicates that the pointer is not meant to point to a memory region that is accessible. But by convention, it is presumed that a pointer points to nothing if it has the null (zero) value.

You may use a "if" statement to check for a null pointer as shown below.

if(ptr)     /* succeeds if p is not null */
if(!ptr)    /* succeeds if p is null */

Pointer arithmetic in C

In C, a pointer is an address, a numeric value. Because of this, you can perform mathematical operations on a pointer in the same way that you can on a numeric value. The ++, --, +, and - arithmetic operators may all be applied on pointers. Let's assume that ptr is an integer pointer that points to the address 1000 in order to better grasp pointer arithmetic. Let's execute the following arithmetic operation on the pointer while assuming 32-bit integers.
ptr++

When the ptr is increased, it will point to the next integer location, which is 4 bytes away from the current position, thus after the aforementioned action, it will point to location 1004. Without changing the value stored at that address, this action will shift the pointer to the next memory location. The above procedure will point to the location 1001 if ptr points to a character whose address is 1000 since the following character will be available at 1001.

Updating a Pointer

In our software, we prefer to use a pointer rather than an array since a pointer may be incremented whereas an array name cannot because it is a constant pointer. The programme below increases the variable pointer to access each additional array entry

The outcome of compiling and running the aforementioned code is as follows:
Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200

A Pointer Being Decremented

The same rules apply when decrementing a pointer, which reduces its value by the amount of bytes appropriate to its data type as illustrated below.

The outcome of compiling and running the aforementioned code is as follows:
 

Address of var[2] = bfedbcd8
Value of var[2] = 200
Address of var[1] = bfedbcd4
Value of var[1] = 100
Address of var[0] = bfedbcd0
Value of var[0] = 10

Comparisons of pointers

Comparing pointers is possible using relational operators like ==,, and >. P1 and P2 can be meaningfully compared if they both point to variables that are connected to one another, such as items of the same array. The programme that follows alters the preceding example in one way: it increases the variable pointer's value as long as the address to which it points is either less than or equal to the address of the array's final element, which is &var[MAX - 1].

The outcome of compiling and running the aforementioned code is as follows:
 

Address of var[0] = bfdbcb20
Value of var[0] = 10
Address of var[1] = bfdbcb24
Value of var[1] = 100
Address of var[2] = bfdbcb28
Value of var[2] = 200