Everything You Need To Know About Pointers In C

Last updated on Jul 28,2020 8.9K Views

Everything You Need To Know About Pointers In C

While learning C, you might have come across the term ‘Pointers’, and often heard that it is a concept hard to understand. Pointers are helpful in various programming tasks and, it is not that difficult to understand. This article will introduce you to pointers in C and tell you why are they not so difficult to learn as they seem.

Following are the pointers covered in this article,

  1. What Is A Pointer?
  2. Reference And De-Reference Operators
  3. Types Of Pointers
  4. How To Use A Pointer

 Let us start with first bit of this pointer in C article,

Pointers In C

What Is A Pointer?

A pointer is a variable which is capable of storing the address of a variable. Now, you might ask what’s the use of that? Basically, the pointer points to the memory location of the variable whose address is provided. This property helps in Dynamic memory allocation, which is an important aspect of programming.

Let’s understand it’s syntax and have a look at an example

Data_Type *pointer_name;

Example: int *ptr;

Following is the next bit in this Pointers in C article

Reference And De-Reference Operators

Before diving deeper into the concept of pointer let’s understand some basics that will help us later on. While using pointers you will definitely use ‘&’ and ‘*’ operators. Now is the time to understand their meaning and use.

First, let’s understand Reference operator often called as ‘Address of’ operator. Using (ampersand) operator with a variable returns us a memory location also known as the address of the given variable.

Example

int *ptr;
int a;
ptr = &a;

The address of variable ‘a’ is stored in variable ptr.

Now, let’s understand the De-Referencing or ‘Value at’ operator which is denoted by an asterisk (*). It helps in retrieving the value from the memory location which is stored in the pointer variable

Example

int *ptr;
int a;
*ptr = &a;
printf("Value of a = %dn", *ptr);

Types Of PointerThe * used while declaring a pointer is not for the purpose of De-Referencing but, tells the compiler that the given variable is a pointer. The above print statement prints the value present in the memory location and this memory location is pointed by the pointer. i.e value of the variable ‘a’.

There are several types of pointers which differ based on the way they are used in a program. We will look at some of the most commonly used types.

Null Pointer 

A null value is assigned to a pointer when you are not sure what address is to be assigned. It can be done by assigning ‘NULL’ value to a pointer at the time of declaration. The value of this pointer is 0.

int *ptr = NULL;

Wild Pointer

A a wild pointer is created by not assigning any value to a pointer variable. It should be used carefully as it may result in unexpected results.

Dangling Pointer 

When a pointer points to a deleted variable or de-allocated memory the pointer is known as a dangling pointer. This pointer points at a non-existing memory location. Let us take a look at the next bit of this Pointers in C article

How To Use A Pointer

Now we have a brief understanding of Reference and dereference operators. In this section, we will understand how to use pointers. Till now we have learned how to declare a pointer and use it for some basic operations. At this point, we have enough knowledge to use pointers for really amazing programming tasks by doing arithmetic operations on them.

Normally, these operations are performed on arrays. Subtracting to 2 addresses can give us the distance between 2 memory locations.

int main()
{
int First_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; /*Our array on which we will perform operations*/
int *ptr; /*Pointer*/
ptr = First_array; /*Assign the address of array to the pointer variable*/
for (int i = 0; i < 10; i++) /*Performing operations*/
{
printf("Value of *ptr variable = %dn", *ptr);
printf("Value of ptr variable = %pnn", ptr);
ptr++;
}
}

Output

Output - Pointers In C - EdurekaTry implementing the code by yourself and perform different arithmetic operations. Let’s use pointers with strings.

#include <stdio.h>
#include <string.h>
int main()
{
char str[]="Hello World";&nbsp; /*Creating an array*/
char *p; /*Pointer*/
p=str;
printf("Printing all the characters in our stringn");
p=str;
for(int i=0;i<strlen(str);i++)
{
printf("%cn",*p);
p++;}
return 0;
}

Output - Pointers In C - Edureka
With this we come to the end of this blog on ‘Pointers In C’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program t
o get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.Implement the above code with different strings and modifications. Now, we have a good understanding of all key concepts related to the pointer.

Got a question for us? Mention them in the comments section of  this blog and we will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.