Pointers In C


Hi friend this is going to be a series of tutorials, as pointer plays a very important role in embedded development. In this tutorial we are going to cover from basics to advance. As we all know pointers are little tricky, but if you practice it properly then a very power full tool for embedded development.  Before going to pointers, first take a look into this basic concept.

C basic tutorials

C basic tutorials

For every variable declared in a program there is some memory allocation. Memory is specified in arrays of bytes, the size of which depending on the type of variable. For the integer type, 2 bytes are allocated, for floats; 4 bytes are allocated, etc. For every variable there are two attributes: address and value, described as follows:

 

A simple very basic program

1  	#include <stdio.h>
2  	main ()
3  	{
4  	   int i, j, k;       //A
5  	   i = 10;           //B
6  	   j = 20;           //C
7  	   k = i + j; //D
8  	   printf ("Value of k is %d\n", k);
    }

Explanation

  • Memory allocations to the variables can be explained using the following variables:

memory

  • When you declare variables i, j, k, memory is allocated for storing the values of the variables. For example, 2 bytes are allocated for i, at location 100, 2 bytes are allocated for j at location 200, and 2 bytes allocated for k at location 300. Here 100 is called the address of i, 200 is called address of j, and 300 is called the address of k.
  • When you execute the statement i = 10, the value 10 is written at location 100, which is specified in the figure. Now, the address of i is 100 and the value is 10. During the lifetime of variables, the address will remain fixed and the value may be changed. Similarly, value 20 is written at address 200 for j.
  • During execution, addresses of the variables are taken according to the type of variable, that is, local or global. Local variables usually have allocation in stack while global variables are stored in run time storage.

Points to Remember

  • Each variable has two attributes: address and value.
  • The address is the location in memory where the value of the variable is stored.
  • During the lifetime of the variable, the address is not changed but the value may change.

POINTERS

In this part you will learn the following:

  •  What a pointer is and how it’s used
  • What the relationship between pointers and arrays is
  •  How to use pointers with strings
  •  How you can declare and use arrays of pointers

If you are a little familiar to C language then no need of saying the importance to you but if you are not, then trust me this concept is the most extraordinarily powerful tool in C language.

 Introduction to Pointers

A pointer is a variable whose value is also an address. As described earlier, each variable has two attributes: address and value. A variable can take any value specified by its data type. For example, if the variable ‘i’ is of the integer type, it can take any value permitted in the range specified by the integer data type. A pointer to an integer is a variable that can store the address of that integer.

A simple program on pointer:

1  	#include <stdio.h>
2  	main ()
3  	{
4  	   int i;         //A
5  	   int * ia;      //B
6  	   i = 10;        //C
7  	   ia = &i;   //D
8  	   printf (" The address of i is %8u \n", ia);          //E
9  	   printf (" The value at that location is %d\n", i);   //F
10  	   printf (" The value at that location is %d\n", *ia); //G
11  	   *ia = 50;                                //H
12  	   printf ("The value of i is %d\n", i);                //I
        }

A simple program on pointer

Explanation

  • The program declares two variables, so memory is allocated for two variables. i is of the type of int, and ia can store the address of an integer, so it is a pointer to an integer.
  • The memory allocation is as follows:

 Memory_aligment

  • i gets the address 545456, and ia gets address 545468.
  • When you execute i = 10, 10 is written at location 545456.
  • When you execute ia = &i then the address and value are assigned to i, thus i has the address of 545468 and value is 545456.
  • You can print the value of i by using the format %au because addresses are usually in the format unsigned long, as given in statement E.
  • Statement F prints the value of i, (at the location 545456).
  • Alternatively, you can print the value at location 545456 using statement G. *ia means you are printing the value at the location specified by ia. Since i has the value for 545456, it will print the value at location 545456.
  • When you execute *ia = 50, which is specified by statement H, the value 50 is written at the location by ia. Since ia specifies the location 545456, the value at the location 545456 is written as 50.
  • Since i also has the location 545456, the value of i gets changed automatically from 10 to 50, which is confirmed from the printf statement written at position i.

Points to Remember

  • Pointers give a facility to access the value of a variable indirectly.
  • You can define a pointer by including a* before the name of the variable.
  • You can get the address where a variable is stored by using &.

To improve the quality of blog and articles please do comment, please visit www.facebook.com/zembedded  Please subscribe to our newsletter to receive blog updates directly in you inbox.

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (3)
  1. hermes replica purses says - Posted: May 26, 2013

    hermes replica purses…

    Hi there, I desire to subscribe for this weblog Pointers In C | zembedded to obtain hottest updates, so where can i do it please assist….

  2. jalaja says - Posted: July 18, 2013

    this tutorial is good ……..it helped me

  3. Megna says - Posted: May 26, 2016

    I am working on a PhoneBook using embedded C. I’m not able to retrieve data which i store in real time. Need suggestions. Thank you

Leave your comment

Your email address will not be published. Required fields are marked *