Microcontroller Embedded C Programming Lecture 73| Pointer variables and initialization

  • Post author:
  • Post category:Blog

 

Pointer variables and initialization

 

 

In this article, let’s learn about Pointer variables and initialization. You learned about pointer variables and pointer data types in the previous article. 

And there we did char* address1 = (char*) 0x00007FFF8E3C3824; something like this, as shown in Figure 1.

Figure 1. Pointer variable and initialization
Figure 1. Pointer variable and initialization

 

Here, address1 is called a pointer variable of type char*.  

 

Let’s understand more about the address1 variable definition. So, when the compiler sees this definition, the compiler allocates 8 bytes for this address1 variable to store this(0x00007FFF8E3C3824) pointer, as shown in Figure 2.

Figure 2. Pointer variable definition and initialization
Figure 2. Pointer variable definition and initialization

 

Figure 3. Pointer variable creation and initialization
Figure 3. Pointer variable creation and initialization

 

When the pointer variable is created, in our case the pointer variable is address1. So, when it is created, 8 bytes of memory will be reserved for this variable. Why? Because in a 64-bit machine, a pointer size is 8 bytes. That’s why irrespective of the pointer variable type, the compiler will reserve 8 bytes of memory to store a pointer.

And the initialized pointer is actually stored inside the variable. The address of the pointer variable and the pointer stored in a pointer variable is shown in Figure 3. And that pointer is nothing but one of the computer’s memory locations (0x00007FFF8E3C3824), and it may have some value(01000001). 

 

Figure 4. Pointer variable creation
Figure 4. Pointer variable creation and initialization

 

Briefly, you can visualize something like this. address1 is a created pointer variable and it is initialized to the pointer 0x00007FFF8E3C3824, and 0x1F007FFF8E3C4821 is the address of this address1 variable. And that pointer may be pointing to some data in the computer memory, like 0x85, as shown in Figure 4. So, you can visualize something like this.

 

Figure 5. Pointer variable definition and initialization
Figure 5. Pointer variable definition and initialization

 

So, it doesn’t matter which pointer data type you use for the pointer variable. The compiler will always reserve 8 bytes for the pointer variable irrespective of their pointer data type. It doesn’t matter whether you use char*, int*, or long long int*. So, the compiler will always reserve 8 bytes in this case because our machine architecture is actually 64-bit. 

In other words, the pointer data type doesn’t control the memory size of the pointer variable.

 

Then, what is the purpose of mentioning “pointer data type”? Why do we have to mention this?

Pointer variable and initialization
Figure 6. Pointer variable definition and initialization

 

In a normal variable definition, when I write char temp = 30; Here, the compiler reserves 1 byte for the temp variable to store this value 30. But that is not the case in the pointer variable definition. So, irrespective of the pointer data type always 8 bytes will be reserved for the variable. 

What is the purpose of mentioning the “pointer data type”? Let’s see why. 

Figure 7. Pointer variable definition
Figure 7. Pointer variable definition and initialization

 

The pointer data type decides the behavior of the operations carried out on the pointer variable. The operations are read, write, increment, decrement, and so on.

Basically, this char* pointer datatype is used to control the behavior of the operations that you perform on the address1 pointer variable.

 

Figure 8. Pointer variable definitions
Figure 8. Pointer variable definitions

 

Here are a couple of pointer variable definitions by using different pointer datatypes. Let’s consider the first statement. So, here a pointer variable is defined using char* pointer datatype. The meaning of this variable definition is when the read operation is performed on the address1, then the read operation yields 1 byte of data out of this pointer. So, this actually decides how many bytes of data can be read from this pointer when the read operation is performed. 

The same thing applies to write operation, increment operation, decrement operation.

If you consider the second definition, when the read operation on this pointer variable is performed, the operation is going to yield 4 bytes of data out of this pointer.

In the third definition, the read operation on the address1 variable, in this case, yields 8 bytes of data.

 

Figure 9. Pointer variable definitions
Figure 9. Pointer variable definitions

 

That’s why, in the first definition, the address1 variable is also called a pointer variable to char type data or pointer variable to 1 byte data. 

In the second definition, the address1 variable is also called a pointer variable to int type data or 4 bytes data.

And in the third definition, the address1 variable is also called a pointer variable to long long int type data. 

 

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