Address and pointers are considered as tough to understand. But in my view it is the most simplest part to learn. In
language, address and pointers are the heart of C programming. An embedded C programmer should understand this topic clearly.
If you are interested to do Machine learning (Embedded system) course through online, with certification and proper placement log on to Udacity (use this link for sign up, thus you get 1000 rs discount in your first enrolment).
- 1 What is an address in C programming ?
- 2 What is the importance of byte in address ?
- 3 What is a pointer in C Programming ?
- 4 Click here for Previous chapters
What is an address in C programming ?
Let’s understand what is an address. Everything in real life has some location. For example we keep our bathing soap inside soap case. Soap case is kept in a cupboard. This cupboard has to be in bathroom. Other example could be railways are put to rest in a rail yard or more. day to day example could be that pizza is served in a plate.
Imagine if at all above examples, we change the location of the items such as bathing soap inside a microwave oven or railways are put to rest on the road or pizza is served in a teapot this will make our life impossible. Understand that for our real life examples we find places to keep our items somewhere in a restricted area, like our home, our city
or this world. similarly in C programming or any software program including the embedded application. Each variable used has a location. These locations are available on a ram. Ram stands for random access memory. Your PC has a ram. Similarly any embedded microcontroller has a volatile memory area, where the values will be located.
Look at the below image
The green area represents a ram of PC or of microcontroller. This Ram is divided into thousands of small areas. This area is called as a byte. As each house in your city has an address. Similarly each sight on this Ram has an address. This address is specified as the byte number. This address numbering starts with 0. Thus the first bite on a ram will have address location 0. So while each byte will have incrementing addresses. Now if you are wondering as what a byte means the answer is simple.
What is the importance of byte in address ?
Byte is a collection of eight bits individual bit. It can store binary value 0 or 1. In simple words, if you declare a
variable name temperature and assign value 45 to it. This is what happens on a ram. The green block shown on the below image represents a byte. This byte is located at address 23.
This memory area holds a value 45. Now we know that address indicates location in a memory and value indicates contents of the memory location. Here we can say that address a variable name temperature is 23 and value of variable temperature is 45.
Remember that we can specify address of a variable in any numbering system. As I said, address a variable name temperature is 23. I assumed a decimal number expressed in hexadecimal numbering system as 0X17. Use Windows calculator to convert decimal 23 to hex value. Generally addresses of memory are specified in hex format and values of memory are specified in decimal format. But this should not be the rule in any case provided. You indicate the number system, you have used to specify the same.
What is a pointer in C Programming ?
Let me explain concept of pointers using a problem statement assume that I have declared a variable named marks, of type int. I have assigned value 82 to the marks variable. Can you tell me the address of variable 82. Can you store the address of variable marks to some other variable. Answer to both questions is shown in the below image in blue color text.
Remember that we require pointers to store address of a variable. Hence we have declared a pointer type variable to declare a pointer type variable. You have to add star symbol before the name of variable on the fourth line of the code. Address of variable marks is assigned to pointer variable PTR. Address of any variable can be retrieved by adding an ampersand symbol before name a variable this is also called as PTR. let’s understand the contents of
variable Mark’s and PTR variable marks. Contents value 82 pointer, variable PTR contents address of the variable marks. We as a programmer have no control over assigning addresses. A particular variable address location will be
assigned by the operating system of your PC. But in embedded development most of the cases a programmer can specify as, where to store the temporary values inside the microcontroller Ram.
Marks 1 is a variable of type integer. The fifth line demonstrates to use pointer to retrieve the value of variable to which it is pointing to do this add a star symbol before the pointer variable and assign it to the simple variable to store the value. Thus now marks 1 will hold value 82.
Always remember that you should keep the data type of a variable and that of pointer variable same. So that the reason we have declared marks and PTR of type int.