Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 7, 2022 06:29 pm GMT

C pointers all you need to know

C is one of the most powerful, efficient, and widely-used languages 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 YOUSTART

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 floatif the decimal points is way too longer then it's data type is double.

What if the user store his grade 'A', then the variables 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


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.


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
int main(){
int number = 18;

int* numberPointer;
numberPointer = &number;
printf("
%d", &number);
printf("
%d", numberPointer);
printf("
%p", numberPointer);
return 0;
}

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("
%d", *numberPointer);

output:
18

Image description

WHY WE SHOULD USE THESEPOINTERS

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 CLEAREDNOW:



Read the code with the below illustrative image
/#include
int main(){
int number = 18;
int* numberPointer;
int** doublePointer;
numberPointer = &number;
doublePointer = &numberPointer;
printf("
%d", &number);
printf("
%d", *doublePointer);
printf("
%d", *(*doublePointer));
printf("
%d", **doublePointer);
return 0;
}

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 pointerbut 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!!


Original Link: https://dev.to/questionmarkblog/c-pointers-all-you-need-to-know-2fgi

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To