DEV Community

quest!on mark
quest!on mark

Posted on • Updated on

C pointers all you need to know

C is one of the most powerful, efficient, and widely-used language in today's world even though it is one of the oldest(since 1972 -Dennis Ritchie), it is still used in

  1. Building operating systems - Microsoft Windows kernel
  2. Embedded systems
  3. Compilers and video games.

In the world of C, pointers are an essential tool for developers. These pointers directly deals with our system's memory which is a great advantage and this gives us strong foundation like literally how the program and storage works together.

But now modern programming languages takes away the function of pointers from the developer's direct control(as advancement!) which makes them to lose fundamental knowledge of programs in memory management.

BEFORE YOU START

If you are a beginner then you must know about variables and data types before commencing otherwise you can skip this part.


VARIABLES

In the layman language variable means something it tends to change which is not constant always.

In a rental house the person who lives there are variables similarly here the rental house is a memory unit and the person who lives in it are the data which will vary as user changes.

Definition: Variables are containers for storing data values. A variable is a name given to that memory location which stores the data.

A variable has four attributes

  1. name.
  2. address
  3. value
  4. type


DATA TYPE

In C the variable's data type should be given to allocate the memory.

If we save the age in a variable it's data type is integer(INT). If my mark 81.5 is stored then it's data type is float…if the decimal points is way too longer then it's data type is double.

What if the user store his grade 'A', then the variable's data type is character(CHAR).If user name is stored which is a group of characters for example "Rowan Atkinson" then it's stored as character array, where array is a data structure in which we can store same type of data.

example:
 int arr[] = {1, 2, 3};
 int arr[] = {1, 'A', 1.2};👎

To store different data types we use structures.

STANDARD LIBRARY

<stdio.h>
Standard Input Output library has the input and output functions.

input function : scanf() getc() etc

output function:printf() putc() etc

To use printf()function in our code then we should include the below code in the beginning by convention.

#include<stdio.h>
Enter fullscreen mode Exit fullscreen mode


The #include preprocessor directive is used to paste code of given file(here stdio.h) into current file.
 
Now let us uncover the magic of pointers in c.


POINTERS

Pointer is a special data type where it can only store the address of the variable i. e, address means the location of the variable in the memory.

Declaring a pointer:

char *charPointer; This charPointer is a pointer to a char data type variable.
 int *intPointer; This intPointer is a pointer to a int data type variable.

Image description


In the number variable
data stored: 22
address of number variable: 2000

In the intPointer which is a pointer to a int data type variable
data stored: 2000 //address of the number variable
address of intPointer: 5000


Note:
int data type variable can hold integer(10) value only, not float(10.0).
charPointer and intPointer are the variable names which can be changed as per user needs.


Now we are ready to tackle any type of pointers💪.Let us dive into the code…

Read the code with the below explanation

 #include<stdio.h>
 int main(){
         int number = 18;   

         int* numberPointer;
         numberPointer = &number;

         printf("\n%d", &number);
         printf("\n%d", numberPointer);
         printf("\n%p", numberPointer);

         return 0;
 }
Enter fullscreen mode Exit fullscreen mode

 
output:
 6422036
 6422036
 000000000061FE14


Here 18 is stored(initialised) in the number variable and the address of the number variable is stored in the numberPointer using & operator.

numberPointer = &number;

Image description

%p is one of the format specifier where it is used to print the value in the variable as hexadecimal format(base 16). By convention, memory address is represented in hexadecimal format.


Image description

Image description


To access the value in the number variable using numberPointer the below statement is used. Here the * (deferencing operator) operator uses the value in the numberPointer(the value present in the numberPointer is the number variable's address) and goes to that address then returns us the data present in that address.

printf("\n%d", *numberPointer);

output:
 18

Image description


WHY WE SHOULD USE THESE POINTERS

This can be used to change/update the values of many different variables of the same type. Execution time is also faster in the usage of pointers
 
 The name of the two important operators used so far is

 & -Address of operator

 * -Indirection operator


THE MOST CONFUSING DOUBLE POINTERS IS CLEARED NOW:

Read the code with the below illustrative image

#include<stdio.h>
 int main(){
    int number = 18;
    int* numberPointer;
    int** doublePointer;

    numberPointer = &number;
    doublePointer = &numberPointer;

    printf("\n%d", &number);
    printf("\n%d", *doublePointer);
    printf("\n%d", *(*doublePointer));
    printf("\n%d", **doublePointer);

    return 0;
 }
Enter fullscreen mode Exit fullscreen mode

Image description

output:
6422036
6422036
 18
 18


*(*doublePointer) = *(6422036)
while looking into 6422036 address 18 value is accessed.


Conclusion

This article is done with the basics of pointers

  1. pointers definition and usage
  2. pointers and addresses
  3. single pointer
  4. double pointer  but you shouldn't be done with pointers. Play with them. Best Wishes.  Before I leave, I would like to leave you with the following comic!

Image description

NEVER STOP EXPLORING!!

Top comments (2)

Collapse
 
phlash profile image
Phil Ashby

This is certainly one point of view 😄, one could also use the phrase 'with extreme caution', as errors in handling pointers (in C and other direct memory access languages) are still a major source of vulnerabilities in software (msrc-blog.microsoft.com/2019/07/18...). Of course if you are intending to create software that manages memory, then you have no option but to write in a language that supports direct memory access, the question however is 'how often does one need to create memory management software?' - arguably, just once per operating system design / implementation, saving 99.99%+ of developers from the pain, frustration and embarassment (when they have to fix their CVEs!) of making errors in memory management...

Collapse
 
questionmarkblog profile image
quest!on mark • Edited

Thank you for your insight 😄, we came aware of the vulnerabilities of pointers but gaining in depth knowledge will sort out the issue I guess and helps developer to go a higher level🥇. Coming to pointers in C, it is one of the main concept where everyone should be aware, because of it's usage in DSA and still these are used to build OS and attaining success and so on💪.