Q

Emertxe-News & Blog

Home » C programming » C programming common pointer mistakes
C programming common pointer mistakes

Introduction

Programmer cannot avoid usage of pointer in Embedded Systems. Though there are many C programming common pointer mistakes made by programmers, accessing memory location directly is provided only by pointers. This is a boon for programmers who program an embedded system with a resource constraint. Let us discuss about C programming common pointer mistakes made by programmers, in this blog.

A Pointer is a variable used to store address of another variable. Pointer can be of any type (int, char etc.). The unary operator ‘&’ gives the address of the variable and the de-reference operator ‘*’ provides value stored in the variable pointed by the pointer. We can declare a pointer variable as,
int *ptr;
Where, int is the data type of the variable which is integer here, * is the de-referencing operator and ptr is the variable name. It should be read as ptr is a pointer to integer.

How to store address

For example,

int *ptr;         //pointer variable declaration
int a = 10;       //variable declaration and initialization
ptr = &a;         //address of variable ‘a’ is stored in ptr.
*ptr = 100;       //value of ‘a’ is now 100

C programming common pointer mistakes

1. De-referencing pointer variable

A pointer variable must be de-referenced to obtain a value from the address pointed by it. Programmers forget to add de-reference operator before pointer variable, during de-referencing. Though it doesn’t provide any error, required output will not be obtained. This is one of the C programming common pointer mistakes made by programmers.
In below code, “*” de-reference operator is missing  in printf statement. This causes error and some junk values are getting displayed in output screen.

Error code

#include<stdio.h>
int main()
{
  int *ptr;
  int m = 4000;
  ptr = &m;
  printf("%d",ptr);
}

Fig 1 – De-referencing pointer variable – error

Output

Fig 1.1 – Error output

Correct code

To obtain value from address pointed by the pointer, provide “*” de-reference operator while printing. Required output is displayed in output screen.

#include<stdio.h>
int main()
{
  int *ptr;
  int m = 4000;
  ptr = &m;
  printf("%d",*ptr);
}

Fig 1.2 –  De-referencing pointer variable

Output

Fig 1.3 – Correct output

2. Assigning value to pointer variable

Next common mistake the programmer does is, assigning value of a variable to pointer variable instead of assigning address of the variable. Pointer variable holds address of another variable not the value. Hence address should be assigned. In the below code, variable ‘m’ is assigned to pointer variable, which results in segmentation fault.

Error code

#include<stdio.h>
int main()
{
  int *ptr;
  int m = 100;
  ptr = m;
  printf("%d",*ptr);
}

Fig 2 – Assigning value to pointer variable

Output

Fig 2.1 – Error output

Correct code

In the given example, we could find address of variable ‘m’ (i.e.) “&m” is getting assigned to pointer variable ptr. De-reference “ptr”, value stored in variable ‘m’ will be obtained.

#include<stdio.h>
int main()
{
  int *ptr;
  int m = 100;
  ptr = &m;
  printf("%d",*ptr);
}

Fig 2.2 – Assigning address to pointer variable

Output

Fig 2.3 – Correct output

3. Address of uninitialized variable – assigned to pointer

Initialize the pointer once declared and before using it. If address of uninitialized variable assigned to a pointer variable, it will provide garbage value while de-referencing it. Assign only the valid address to the pointer variable. In the below code, variable ‘m’ is not initialized and address of variable ‘m’ is assigned to the pointer variable where the output is garbage value.

Error Code

#include<stdio.h>
int main()
{
  int *ptr;
  int m;
  ptr = &m;
  printf("%d",*ptr);
}

Fig 3 – Assigning address of uninitialized variable

Output

Fig 3.1 – Error output

Correct code

In the below code, variable ‘m’ is initialized and address of the variable is assigned to pointer variable ‘ptr’. When the pointer variable is de-referenced, value stored in variable ‘m’ is printed in output screen.

#include<stdio.h>
int main()
{
  int *ptr;
  int m = 25;
  ptr = &m;
  printf("%d",*ptr);
}

Fig 3.2 – Assigning address of initialized variable

Output

Fig 3.4 –  Correct output

4. Comparing pointers

Pointer variables comparison is not valid, since they are stored in random memory locations. In the example provided below, if condition itself is error. Comparison is not possible between pointers.

#include<stdio.h>
int main()
{
  char str1[10],str2[10];
  char *ptr1 = str1;
  char *ptr2 = str2;
  if(ptr1 > ptr2)
  {
    printf("comparing pointers");
  }
}

Fig 4 – Comparing two pointers

5. De-referencing NULL pointer

A pointer pointing to nothing is a NULL pointer. De-referencing a pointer which is pointing to no location gives segmentation fault. Therefore, before de-referencing a pointer, programmer should be aware that the pointer is not a NULL pointer. In the below code, pointer ptr is declared and initialized to NULL and “*ptr” which is the value in the address pointed by pointer is supposed to get overwritten as 10. Since ptr is not pointing to any location, we will get segmentation fault.

Error Code

#include<stdio.h>
int main()
{
   int *ptr = NULL;
   *ptr = 10;
}

Fig 5 – De-referencing NULL pointer

Output

Fig 5.1 – Error output

Conclusion

Pointers play a very major role in C programming. They have direct access to memory as they store address of variables. Above mentioned are few C programming common pointer mistakes that programmers do often, while writing a program. There are some operator precedence mistakes made using pointers and increment, decrement operators and while using pointers with arrays etc. To avoid errors and provide efficient output, programmers should learn seven rules of pointers.

Related links:

1. Dangling pointers in C

2. Constant pointer and pointer to constant

3. Void pointers

Happy Learning!!

 

 

 

YOU MAY ALSO LIKE

Emertxe’s Online Training Programs in Embedded Systems & IoT

Emertxe’s Online Training Programs in Embedded Systems & IoT

The reason why Emertxe is the best choice for career advancement: eLearning and Online training programs are the way forward in the COVID-19 disrupted world. Riding along the digital revolution will ensure engineers are future-ready with skills to not only secure but...

Our Training Programs for Freshers

Our Training Programs for Freshers

Introduction: Emertxe is the leading training institute in Bangalore for Embedded Systems and IoT domains. It is a pioneer in training freshers since 2003 by providing excellent placement opportunities for freshers. Over the years 70000+ students have made their...

Q