×

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the variable that it is pointing to. A pointer is also used to refer to other pointer functions present. Increment and decrement operations can be done on the pointer variable; that is, it can point to the next or the previous memory location. The vital purpose of the pointer is to save the memory space and get faster execution of the programs and make it more efficient.

Pointers in the C standard are fun and easy to learn and will be a massive threat if not understood and appropriately implemented. Many tasks within the programs, such as dynamic memory allocation, cannot and will not be performed without the intervention of the pointers.

Hence, it is a necessary operation that every programmer and developer learn. Everymostlyinvolves variable is a memory location in any programming language, and every memory location has its address defined in its way.

Like any other constant or a variable, one must always declare a pointer before using it to store the variable’s address. It can be accessed using the operator named ‘ampersand’ (&) that denotes the address in the memory. Pointers are declared using the asterisk symbol (*).

Syntax1

datatype *variable_name 1;
  • The above-mentioned syntax is to declare a pointer.
  • The declaration must be a valid datatype present in the C standard
  • The variable_name can be anything that represents the name of the pointer variable.
  • The ‘asterisk’ (*) is used to declare the pointer.

E.g.:

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

Note:The difference between the pointer of several data types is that the data type of that variable or the pointer’s constant points to.

Syntax2

 dataype variable_name1, *variable_name2;
 variable_name2 = &variable_name1; 

The above-mentioned syntax is to assign the address of the variable to a pointer.

E.g.

 #include <stdio.h>
 #include <conio.h>
 int main ()
 {
 int  var1;
 char var2[10];
 printf("The address of the var1 variable: %x\n", &var1  );
 printf("The address of the var2 variable: %x\n", &var2  );
 return 0;
 } 

Output

Pointers in C

Address of the operator (&)

The symbol (&) returns the address of the variable. But it should be used along with %u in order to display the address of that particular variable.

E.g.

 #include <stdio.h>
 #include <conio.h>
 int main()
 {
 int n = 10;
 printf (“The value of the number ‘n’ is %d \n and the address of the number ‘n’ is %u”, n, &n);
 return 0;
 } 

Output

Pointers in C

Steps on how to use pointers

  1. Define the pointer variable in the beginning of the program.
  2. Assigns the address of that variable that was defined earlier to a pointer.
  3. Access the value of that address that was assigned and available in the pointer variable.
  4. It is done using the unary operator (*) asterisk that always returns the value of the variable that is being located at the address which is specified by its operand.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 int main ()
 {
 int  variable = 30;   /* actual variable declaration (step 1) */
 int  *ptr;        /* pointer variable declaration (step 2) */
 ptr = &variable;  /* store address of var in pointer variable (step 3) */
 printf("The address of the variable is: %x\n", &variable  );
 /* address stored in pointer variable */
 printf("The address stored in the ptr variable is: %x\n", ptr );
 /* access the value using the pointer */
 printf("The value of the pointer *ptr variable is: %d\n", *ptr );
 return 0;
 } 

Output

Pointers in C

Uses of a pointer in the applications

Pointers are one of the essential operations and have many applications in the programming language such as;

Functions, structure, and arrays.

Pointers help reduce the code; that is, it diminishes the lines of code required to run an operation. It is most used in functions, arrays, and structures as well.

Dynamic memory allocation.

In the C standard, there exists a memory allocation that involve dynamically allocating the memory. It uses the pointer theory in them.

Syntax

Below is Syntax for the dynamic memory allocation:

 malloc() - “ptr =  (data type*) malloc(byte-size);”
 calloc() - “ptr =  (data type*) calloc(n, element-size);” 

Note: free(), and realloc() requires the concept of pointer inorderfor them to be implemented.

Advantages of using the pointer

  • A programmer can return the multiple values from the function using the pointer.
  • Using a pointer drastically reduces the code and constantly improves the performance, and it is also used to revive the trees, string, and so on. They can also be used in concepts of arrays, functions, and structures.
  • It makes a developer access the memory location at any point in time from the computer’s memory.

Disadvantages of using a pointer

  • Segmentation fault is caused due to uninitialized pointers.
  • Dynamically allocated block in the C standard should be freed explicitly.  Else, it will lead to a memory leak.
  • The pointers are usually slower than the normal variables.
  • If pointers are updated with incorrect values, it might lead to memory corruption.

Related Topics

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.