Pointers in C Programming - ProgrammerTech
Language:

Search

Pointers in C Programming

  • Share this:
Pointers in C Programming

Pointers in C Language

Pointers , Today we will learn about a very important feature in C language and this feature distinguishes this language from other languages. Because it represents the programmer to organize memory more and better during program execution compared to other languages. And this is what made C an advanced language that can be used in programming operating systems or other advanced software. This feature is called pointers.

 

Store values ​​in memory

You can think of memory on computers as a large array containing 1 byte of data. If we say that a particular device has the capacity of RAM to be 2GB and gigabyte means one billion, this means that the RAM. This device can store 2 billion bytes. And, of course, the data in memory may be from storage, either from the operating system, or from a specific program that is being executed.

How is a specific memory location accessed and values ​​stored or retrieved?

Each location in memory has a specific address that is used to access this location at any time. For example, we focus on the following.

1-86.webp

In the previous figure, we notice that the site with address 0 contains the value 23, the site with address 1 contains the value 26, and the site with address 2 contains the value 27 and so on...... etc. Let's recall together what we learned earlier about how variables are stored in memory, let's look at the following figure.

2-79.webp

When we define a variable, a specific place in memory is reserved for it in this variable, and it is mentioned here that the data type is int, which is of 2 bytes in size. It will only occupy two places in memory and not one, but for the sake of simplicity here in this lesson we will assume that it is only one place so far. If we call this variable by its name, the value stored in it will be returned to us.

int x = 23;
printf("%d", x);

In the previous code, int x is equal to 23, then we print the x and it will print 23, but how do we retrieve it. The address of this location, the location of the variable x where the value is stored instead of the value itself? This is done by adding & before the variable name

int x = 23;
printf("%p", &x);

And of course the & sign differs from the & used in logical operations, as it has another meaning here, and d has been changed to p means. We want the location of this variable and the letter p is an abbreviation of pointer and it means the place where the variable lives.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int x = 23;
    printf("%p", &x);
    return 0;
}

We create a scalar int variable with a name x and a value of 23 and then we ask to print where or where this variable resides in memory. Via %p and then put & before the variable name, when we run this program we will get the result.

0x7fffc36209ec

We have been given the location 0x7fffc3fd286c where the variable x resides and is of type hexadecimal. That is, the numbers are from 0 to F and the beginning of the site was 0x and it is not within the site. For this variable, the hexadecimal system can be known by watching the following video.

 

Storing variable addresses in C pointers

We learned a while ago about how to print the address of a variable, but how can we use this address other than just printing it. Can we store this address for later use, as in regular variables? Yes, here comes the role of pointers, which are data types that point to a specific address in memory. It differs from the method of defining a normal variable because it contains an asterisk next to the variable name.

This asterisk indicates that this variable is intended to store addresses and not values, i.e. unlike a regular variable that is used to store values. And in the previous picture we have int x equals 9 and x is a normal variable that has a normal value of type int and int*pointer equals 1 is a pointer. It marks the address 1 and we cannot write the pointer like this, and we will see this later. Since the programmer does not see the random access memory in the computer and does not know what data is stored. Where and where it is stored, it is impractical to specify the address directly as a normal value.

int x = 23;
int *pointer = &x;
printf("%p", pointer);

If we want to store the address of variable x in the pointer called pointer, we define the pointer by placing an asterisk * before its name and then we specify its value, which is the address of variable x, where we use the & sign to return the address of this variable.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int x = 26;
    printf("X value = %d\n", x);
    printf("X address = %p\n", &x);
    return 0;
}

We created a scalar variable named x and its value is 26, then we asked to print the variable as it is in the first operation. And in the second printing process, we asked for the location of this variable in memory, and when running. With this program, we will get two results as follows

X value = 26
X address = 0x7ffde9397b4c

The first line is the value of the variable as a normal value, and the second line is where this variable is located in RAM.

 

Storing a memory address in a variable by pointer C language

We can write the location of the variable in memory with another variable, and this is what we will learn about in the following example.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int x = 26;
    printf("X value = %d\n", x);
    printf("X address = %p\n", &x);
    int *xPointer = &x;
    printf("X Pointer = %p", xPointer);
    return 0;
}

We created a scalar variable named x and its value is 26, then we asked to print the variable as it is in the first operation. In the second printing process, we asked for the location of this variable in memory, and then we created a variable called Pointer. And we put an asterisk before the name, then we wrote that the value of this indicator is x, and we put the & sign before the variable name. Then we asked to print this variable as a normal variable and when we run this code, the result will be

X value = 26
X address = 0x7ffffb1fc864
X Pointer = 0x7ffffb1fc864

As we noticed in the beginning, the value of the variable was printed for us as a normal value, which is 26, and then the location of this variable was printed for us. In memory and finally we store the location of the variable in a regular variable.

 

Modify values ​​by pointers in C

Modifying Values ​​with Pointers So far we have learned how to define pointers and return the value in the addresses they refer to.

But can we modify these values ​​and not just roll them back?

In the last example in the previous lesson, we created the variable x=26 and we printed the variable x and its value was 26. Then we printed the private address in x and the address was 0x7ffffb1fc864 of type hexadecimal and then we stored this address. To store the address of a variable, a pointer variable must be created, and a regular variable cannot be created, as in x.

Then we print the value of this variable and this variable holds the value of an address so we don't put d because we don't print a decimal number. Rather, we print an address and this address is of hexadecimal type and not of the regular numbers from 0 to 9, and then we access the value inside the address through the star. And we said that there is a difference when the star when defining and the star when using, the star when defining means that we are defining a variable of type pointer. An asterisk when used means that we want to access the value that resides at this pointer's address.

Example

We will rely on the example that was at the end of the previous lesson, we will first adjust the value of x to see the result.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int x = 26;
    printf("X value = %d\n", x);
    printf("X address = %p\n", &x);
    int *xPointer = &x;
    printf("X Pointer = %p\n", xPointer);
    x = 23;
    printf("The value inside the title %p = %d\n", xPointer, *xPointer);
    return 0;
}

We see that x initially was equal to 26 and when we changed the value of x to 23, the value of x changed in the last print function at *xPointer when we called it to print. When we print the value inside the address *xPointer, the new value of x is printed and the result is.

X value = 26
X address = 0x7fff0283d794
X Pointer = 0x7fff0283d794
The value inside the title 0x7fff0283d794 = 23

The value of x can also be changed through the Pointer, as we have full access through the address to the variable that inhabits this address. For example, we have a home address. We can change what's inside this house, as long as we have the address by accessing what's inside this house.

Example

And simply we write *xPointer as it was printed below and we change its value as follows

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int x = 26;
    printf("X value = %d\n", x);
    printf("X address = %p\n", &x);
    int *xPointer = &x;
    printf("X Pointer = %p\n", xPointer);
    *xPointer = 1995;
    printf("The value inside the title %p = %d\n", xPointer, *xPointer);
    return 0;
}

Based on the previous example, we changed the value of x through the Pointer and changed the value to 1995. Then we printed and printed the place of residence of this variable from memory and equal to the new value, which is 1995. When we run the code, we will find the following result

X value = 26
X address = 0x7ffc84dd0814
X Pointer = 0x7ffc84dd0814
The value inside the title 0x7ffc84dd0814 = 1995

Up to here we talked about everything related to the basics of indicators and from that we can see that they are very important. And through it the programmer can access the memory and organize it however he wants, and this is necessary for writing advanced programs such as OS operating systems and drivers, as well as other programs that require high efficiency in memory management. And we can say that pointers are one of the things that gave C language its power as it was and still is the most requested programming language at all.

Example

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int x = 7, *cs = &x;
    printf("%d\n", *cs * *cs);
    return 0;
}

Here we created a variable of type integer and called it x and its value is 7 and created Pointer with it on the same line. This pointer will point to int and we note that the name of this pointer is *cs and the star we put it next to the name of this pointer means that this variable is a pointer of type int and its value is equal to the address x. And the word address we abbreviated with the symbol & to indicate that this is the address located in x and we printed and wrote the first *cs means access to the value located inside the address *cs. And we also set a star to make the operation a multiplication, then we wrote *cs, as if we were saying x multiplied by x, and when we run the code, its result will be 49 as follows.

49

Example

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
   int x = 7, *cs = &x;
    printf("%d\n", *&x);
    return 0;
}

We created a variable of type integer and called it x and its value is 7 and we created Pointer with it on the same line and this pointer will point to int. We note that the name of this pointer is *cs and the asterisk we put it next to the name of this pointer means that this variable is a pointer of type int and its value is equal to the address x. And the word address we abbreviated with the symbol & to indicate that this is the address located in x, then we printed *&x meaning open what is inside the address. And look what is the value inside this address x and when we see the result will be as follows

7

When we see * after & then the asterisk cancels the effect of the &, so if we switch here between & and the asterisk &*x, it will give us an error. It is not possible to put a star before x because x is a variable of type int, and when you put a star directly before the variable, it means that the variable. The one that comes after the star is of the pointer type, i.e. house, place, address, when we put a star it means look inside this address.

 

Arithmetic operations on pointers in C

Pointer Arithmetic Calculations Did you know that the C language allows us to perform arithmetic operations on Pointers. We will learn how to do this through the code, now all you have to do is create a new project in your editor and follow the following code.

Example

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int array[4] = {20, 30, 40, 50};
    printf("array[0] = %d\n", array[0]);
    printf("array[1] = %d\n", array[1]);
    printf("array[2] = %d\n", array[2]);
    printf("array[3] = %d\n", array[3]);
    return 0;
}

We created a numerical array of type int and its name is array and it has four elements which are (20, 30, 40, 50). We print these values ​​on the screen by defining them via the index of each element. The output window will be.

array[0] = 20
array[1] = 30
array[2] = 40
array[3] = 50

We see that it prints all the values ​​inside this array for us.

 

Print addresses for values. Print addresses

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int array[4] = {20, 30, 40, 50};
    printf("array[0] = %p\n", &array[0]);
    printf("array[1] = %p\n", &array[1]);
    printf("array[2] = %p\n", &array[2]);
    printf("array[3] = %p\n", &array[3]);
    return 0;
}

We created a numerical array of type int and its name is array and it has four elements which are (20, 30, 40, 50). We print the labels of these values ​​on the screen by specifying the labels via the index of each element. Putting an & before the array name to indicate that we want a title and using the letter p. The output window will be.

array[0] = 0x7ffe9e957190
array[1] = 0x7ffe9e957194
array[2] = 0x7ffe9e957198
array[3] = 0x7ffe9e95719c

We notice in the first address 0x7ffe9e957190 at the end that the starting point was 0. Then the second point was at 4 because the size of the int as we said in the previous lesson is 4 bytes, which means that each element. It will be 4 bytes away from the element before it and at the third point it was 8 and the fourth c ie 12 in hexadecimal.

 

Using pointers to access addresses

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int array[4] = {10, 20, 30, 40};
    int *DataCs = &array[0];
    printf("address array[0] = %p\n", DataCs);
    printf("address array[1] = %p\n", DataCs + 1);
    printf("address array[2] = %p\n", DataCs + 2);
    printf("address array[3] = %p\n", DataCs + 3);
    return 0;
}

We created a numerical array of type int and its name is array and it has four elements which are (20, 30, 40, 50). And we defined a variable named DataCs and we defined it as a pointer and we put a star before it and we marked the first variable below. That is, we made this indicator bear the value of the first element, and we printed these values ​​on the screen.

array[0] = 0x7ffc9cf3bd40
array[1] = 0x7ffc9cf3bd44
array[2] = 0x7ffc9cf3bd48
array[3] = 0x7ffc9cf3bd4c

We have accessed the elements without using the array and using the pointer on the array.

 

Use pointers to access items

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int array[4] = {10, 20, 30, 40};
    int *DataCs = &array[0];
    printf("address array[0] = %d\n", *DataCs);
    printf("address array[1] = %d\n", *DataCs + 1);
    printf("address array[2] = %d\n", *DataCs + 2);
    printf("address array[3] = %d\n", *DataCs + 3);
    return 0;
}

We defined a variable named DataCs and defined it as a pointer and put a star before it and marked the first variable below. That is, we made this indicator carry the value of the first element, then at the bottom at the second value we put +1, and at the third value +2 and the fourth value +3. This and when we ran the code it worked normally but there is an error, we will explain the error after the image of the following output window.

address array[0] = 10
address array[1] = 11
address array[2] = 12
address array[3] = 13

We notice that the first value is a constant 10, and the next one is a sequence of 11, 12 and 13. This means that we have the first address array[0] is 10, then at the bottom we add to the address 1 in DataCs + 1 means that we have access to the address DataCs. And we added 1 to it, so the value became 11, then the same thing for the rest of the values. And to reach the values ​​without increasing 1 on them, we put parentheses as follows.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int array[4] = {10, 20, 30, 40};
    int *DataCs = &array[0];
    printf("address array[0] = %d\n", *DataCs);
    printf("address array[1] = %d\n", *(DataCs + 1));
    printf("address array[2] = %d\n", *(DataCs + 2));
    printf("address array[3] = %d\n", *(DataCs + 3));
    return 0;
}

And when this code is executed, it will print out the elements of the array and access them. By using pointers, the output window for the code is.

address array[0] = 10
address array[1] = 20
address array[2] = 30
address array[3] = 40

Tags:
Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان.