Stay in touch with EE Times India

EE Times-India > Embedded

Embedded

# Programming: Managing the risk of pointers

Posted: 10 May 2016     Print Version

Keywords:Pointers  C language  programming language  assembly language programmer  memory

In either case, there should be very explicit commenting to explain this bizarre programming.

Pointers and arrays
Arrays in C are quite straightforward – they are just a named series of variables in contiguous memory locations. We can declare a 5-element array and set the third element (the indices start at 0) to a value thus:

unsigned array[5];
array[2] = 99;

The relationship between arrays and pointer is interesting. As an array is a sequence of memory locations, it would seem logical to use a pointer to access an element of an array. So, we can write:

unsigned *pointer;
pointer = &array[0];
*(pointer+2) = 99;

The pointer is set to point to the first element of the array and then pointer arithmetic is used to index to the third element. A neater way to write this code would be to exploit that fact that the array name by itself is just a (constant) pointer to the array. So we could write:

pointer = array;

which would yield the same result.

The pointer arithmetic syntax is rather messy, which is why the C language has the [ ] operators. These brackets are just a neater way to do the pointer manipulation. So, you could write:

pointer[2] = 99;

Although this is valid, it is strongly recommended that the bracket notation is only used with array names, not pointers. This is simply to preserve clarity.

Given this information, there are two ways to dynamically initialise our array:

unsigned array[5];
int i;

for (i=0; i<5; i++)
{
array[i] = 99;
}
or
unsigned array[5];
unsigned *pointer;
int i;

pointer = array;
for (i=0; i<5; i++)
{
*(pointer++) = 99;
}

Which is best? The former is certainly clear and easy to understand, but there are circumstances – usually with less trivial code – where the latter might make sense.

Conclusions
Pointers are a powerful and, hence, potentially dangerous feature of the C language. Many developers are unclear about their use. It is, therefore, good practice to avoid their use, except where their power is really necessary. Using a pointer just to show off your programming skills is very unwise. However, pointers do behave logically and, used well, result in efficient code, which may be rendered quite readable and maintainable with care. Under very specific circumstances, C programmers can override the type-based functionality and work in terms of CPU memory addresses. This practice should be exercised with extreme caution.

Colin Walls has over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, Colin is an embedded software technologist with Mentor Embedded (the Mentor Graphics Embedded Software Division), and is based in the UK.

1 • 2

Comment on "Programming: Managing the risk of po..."
Comments: *  You can enter [0] more charecters.

Top Ranked Articles

Webinars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

Search EE Times India
Services

﻿