Read and Write operation on pointers
In this article, let’s understand the Read and Write operations on pointers.
Read operation on the pointer
All you have to do is, you have to dereference the pointer. *address1 is called dereferencing a pointer variable, as shown in Figure 1. You must use ‘*’ in front of the pointer variable name. If you do char data = *address1, the value at this address(0x00007FFF8E3C3824) will be fetched, and it will be stored in this variable data.
In the pointer context, the ‘*‘ symbol is also called the “Value at Address” Operator. So, the compiler will fetch 1 byte of data from the pointer(0x00007FFF8E3C3824) and store that data into a “data” variable.
Why will the compiler fetch 1 byte of data?
Because its type is char*. That’s why only 1 byte will be fetched.
Let’s take an example. Here, address1 is a pointer variable, and it is initialized to this pointer, 0x00007FFF8E3C3824, and that pointer currently has the value 0x85.
So, when you do char data = *address1, *address1 is actually nothing but you are dereferencing this address1 pointer variable. That means you are dereferencing 0x00007FFF8E3C3824. Dereferencing means fetching the value which is present at this pointer, which is 0x85. So, 0x85 will be stored in the data variable. That’s what we call as dereferencing.
Write operation on the pointer
For example, if you want to write data into 0x00007FFF8E3C3824 this pointer, then you dereference that pointer(address1*), and you replace the value with a new value(0x89). So, *address1 = 0x89 is called dereferencing a pointer to write new data. Here, we are writing new data into the memory location, which is addressed by this pointer, 0x00007FFF8E3C3824.
For example, 0x00007FFF8E3C3824 is a memory location address that currently has the value 0x85 in the memory location. So, once you do *address1 = 0x89, it is like you are replacing the content of that memory location. So, you are replacing the value 0x85 with a new value 0x89. Now it becomes 0x89. So, that is what we call a write operation on the pointer. All these operations you do by using the pointer variable. Instead of directly using the address1 here, you are using a pointer variable.
Before ending this article, I would like to give you one exercise. This exercise you have to do and I’m going to solve this exercise in the next article. The exercise is that you must follow these steps to do certain things.
Exercise:
- Create a char type variable and initialize it to value 100.
- Print the address of the above variable.
- Create a pointer variable and store the address of the above variable.
- Perform read operation on the pointer variable to fetch 1 byte of data from the pointer.
- Print the data obtained from the read operation on the pointer variable.
- Perform write operation on the pointer to store the value 65.
- Print the value of the variable defined in step 1.
I was hoping you could do all these steps, and by doing these steps, you will learn many things about how the pointer behaves. I will explain this exercise to you in the following article.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1