Microcontroller Embedded C Programming Lecture 71| Pointers in ‘C’

 

Pointers in ‘C’

 

 

Pointers

  • Pointers are one of the essential programming features which are available in ‘C’ programming language. 
  • Pointers make ‘C’ programming language more powerful.
  • Pointers are heavily used in embedded ‘C’ programming to 
    • Configure the peripheral register addresses
    • Read/write into peripheral data registers
    • Read/write into SRAM/FLASH locations and for many other things.

From this article onwards, let’s study pointers step by step.

 

What is a pointer?

A pointer in the C programming language is a variable that holds the memory address of another variable. It essentially “points” to the location in memory where the value of another variable is stored.

 

Now let’s explore that. 

Pointers in C programming
Figure 1. Memory organization in a computer memory

Consider this a memory organization in computer memory, as shown in Figure 1. Here you see different memory locations of computer memory, and each location can hold 1 byte of data.  

The left-hand side displays memory location addresses, like 0x00007FFF8E3C3821, 0x00007FFF8E3C3822, 0x00007FFF8E3C3823, and so forth. Each memory location is uniquely identified by an address, which is precisely what a pointer encapsulates: a memory location address. Notably, this address, such as 0x00007FFF8E3C3821, effectively points to the actual data, here denoted as 01110111, residing in the associated memory slot.

By using this pointer, you can do various operations. What are the operations possible? So, you can write into this(0x00007FFF8E3C3821) pointer. That means you can write a new value into this(01110111) memory location. You can read from this(0x00007FFF8E3C3821) pointer, which means this value(01110111) can be read in the program using this pointer.

You can increment the pointer and go to the next memory location, or you can decrement the pointer and go to the previous memory location. All these operations can be done on this pointer. Hence a memory location address is called a pointer.

Exploiting the Power of Pointers

Harnessing this pointer’s might, various operations become feasible:

  • Write Operation: This pointer, such as 0x00007FFF8E3C3821, can be used to write new values, overwriting the existing data (01110111) in that memory location.
  • Read Operation: By employing the same pointer, the value (01110111) can be read from the associated memory location programmatically.
  • Pointer Manipulation: The pointer can be incremented, guiding it to the subsequent memory location, or decremented to navigate to the preceding memory location. These dynamic maneuvers all center around the pointer.

 

Variability in Pointer Sizes

The size of a pointer, which corresponds to the memory location address size, varies based on the underlying hardware architecture. In a 64-bit system, the pointer size occupies 8 bytes (64 bits).

By comparison, a 32-bit ARM microcontroller features a 4-byte (32-bit) pointer size, and PIC18 microcontrollers sport 2-byte pointers. Regardless of these differences, the fundamental concept remains consistent: a pointer embodies a memory location address.

 

Upcoming: Storing Pointers in Programs

In forthcoming articles, we will delve into the intricacies of storing and utilizing pointers within programs, uncovering their potential in enhancing ‘C’ programming efficiency and versatility.

 

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.