Pointer in C

Pointer in C
Techiio-author
Written by Shuvhojit DebFebruary 3, 2022
10 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

Today we'll discuss what a pointer is and what's its use.

What is Pointer in C?

The Pointer in C is a variable that stores the address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of a pointer is to save memory space and achieve faster execution time.

Different Ways of Accessing Variable Address in C

Let us try to know what are the different ways by which we can print the address of a variable in C.

There are two ways by which we can actually print the address of a variable. Those two methods are:

  • By using ampersand ( & ) method
  • By using pointer

Let us look into each method with an example

1. By using an ampersand (&) method

In this method, we will be using the concept of the ampersand to print the address of a variable.

Let us look this with an example

#include <stdio.h>
int main () {
double varNumValue= 10.2;
char varTextValue[10] = “abc”;
printf("The address of variable varNumValue: %x\n", &varNumValue  );
printf("varTextValue variable address is : %x\n", &varTextValue  );
return 0;
}

Now, copy the above code snippet and run it

It will show the following output:

The address of variable varNumValue: a0665a48
The address of variable varTextValue is : a0665a3e

Although, one might see the different value which is assigned randomly while running the example

The above-mentioned example is not much used, but it is worth knowing. Now let us look to the other concept which we are discussing in this topic.

2. By Using Pointer

This is a modern approach to access the address of a variable. Before using a pointer, let us know the general syntax of declaring a pointer.

  • The general syntax of declaring a pointer – A common syntax which we use for declaring a variable is:
data_type *variable

This is a common way to declare any pointer in C, here data_type represents the type of the variable whose address needs to be stored. * denotes that the variable declared is a pointer. a variable is simply used for accessing the value

A simple example of this is:

double *var_text

After discussing the concept of the General syntax of declaring a pointer, let us know how we can use a pointer in programming.

To use a pointer in C, basically, one needs to follow the following three steps:

  • Defining of pointer variable
  • Assigning the address of the variable whose address we want to hold in the pointer variable
  • Now, once we have a pointer variable with the address, we can again retrieve the value of the variable from the address stored in the pointer. This could be done using is simply one of the unary operators in C.

Let us use the above-mentioned steps with an example, and then we will explain this example step by step.

Example

#include <stdio.h>
int main () {
int  varNumValue = 10;
int  *ipointervarNumValue;
ipointervarNumValue = &varNumValue;
printf("Address of the variable varNumValue is: %x\n", &varNumValue  );
printf("Address stored in the variable ipointervarNumValue is: %x\n", ipointervarNumValue);
printf("Value of the variable *ipointervarNumValue is: %d\n", *ipointervarNumValue );
return 0;
}

Now, copy the above code snippet and run it

It will show the following output:

Address of the variable varNumValue is: bf7b7f94
Address stored in the variable ipointervarNumValue is: bf7b7f94
Value of the variable *ipointervarNumValue is: 10

Now, let us try to understand the above example

The line int varNumValue = 10; simply declares a variable with value 10

The second line that is a line next to it: int * ipointervarNumValue; is simply declared to store the address of the variable

The next code snippet, which is: ipointervarNumValue = &varNumValue, is used to hold the address of the variable. Here, we are using the concept of an ampersand, which we discussed earlier in the article

Now, we can simply get the value and use a pointer in our code snippet

Now, let us look into different types of a pointer in C

Types of Pointer in C

Different types of pointers in C are following:

1. Null Pointer

We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0.

Following program illustrates the use of 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;
}

Output:

The value inside variable p is:
0

2. Void Pointer

In C programming, a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable.

Following program illustrates the use of 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;
}

Output:

The size of pointer is:4

3. Wild pointer

A pointer is said to be a wild pointer if it is not being initialized to anything. These types of C pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers.

Following program illustrates the use of wild pointer:

#include <stdio.h>
int main()
{
int *p; 	//wild pointer
printf("\n%d",*p);
return 0;
}

Output:

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

Conclusion

Pointers in C are used to point to the address of the variable. These variables are used for the dynamic allocation of memory in C. These variables are declared with an asterisk so as to show that the variable is a pointer. These are used in the header file in programming.

C Language
C Pointer
Variables
C Program
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance