Microcontroller Embedded C Programming Lecture 74| Read and Write operation on pointers

  • Post author:
  • Post category:Blog

 

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.

Figure 1. Read operation on the pointer
Figure 1. Read operation on the pointer

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 adatavariable. 

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.

Figure 2. Example
Figure 2. Example

 

 

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. 

Read and write operations on pointers
Figure 3. Write operation on the pointer

 

Read and write operations on pointers
Figure 4. Example

 

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:

  1. Create a char type variable and initialize it to value 100.
  2. Print the address of the above variable. 
  3. Create a pointer variable and store the address of the above variable. 
  4. Perform read operation on the pointer variable to fetch 1 byte of data from the pointer. 
  5. Print the data obtained from the read operation on the pointer variable. 
  6. Perform write operation on the pointer to store the value 65.
  7. 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

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply