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

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:

Source: Safalta.com



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

Free Demo Classes

Register here for Free Demo Classes

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
                                                                                                                                                                                                                                                                                                                                                

                                                                                                                                            

Related Article

IDBI Bank Recruitment 2024: आईडीबीआई में जेएएम और एएओ के लिए निकली भर्ती, जानें कौन कर सकता है आवेदन

Read More

CAT 2024 Tomorrow: Exam day guidelines, timings, do's and don'ts; Check the list of prohibited items here

Read More

CBSE Single Girl Child Scholarship 2024 Registration window open now, Check the eligibility criteria and more

Read More

UP Police Constable Result 2024: Candidates demand raw scores, question transparency; Check the latest update

Read More

UP Police Result: यूपी पुलिस भर्ती के अभ्यर्थी कर रहे अंक जारी करने की मांग, बोर्ड ने दी प्रतिक्रिया

Read More

RRB ALP Admit Card: 25 नवंबर की एएलपी भर्ती परीक्षा के लिए जारी हुआ प्रवेश पत्र, जानें डाउनलोड करने का तरीका

Read More

CHSE Odisha Class 12 date sheet 2025 out now; Check the exam schedule here

Read More

CBSE Date Sheet 2025: सीबीएसई बोर्ड कक्षा 10वीं 12वीं की डेटशीट हुई जारी, यहां देखें पूरा शेड्यूल

Read More

CBSE Date Sheet 2025: Class 10, 12 timetable at cbse.gov.in awaited, Check the latest update here

Read More