×

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

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Nested loop in C

Definition of Nested Loop 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...

3 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.