Newbie pointer question

Jul 25, 2006 10 Replies

Hi all. I was wondering what the advantage of using pointers are( Programming in 'C' ).For instance : I have a simple embedded controller application , turning a few io on and off , maybe some serial comms etc. I can create variables (Global or local) and then access them directly , or can access them via pointers , but is there any advantages. Does the compiler generate more efficient code? In simple applications "is" there any advantage to using pointers? Can any one "Point" me in the right direction , haa haa. Cheers Rob



If you're simply creating pointers to variables, then using a pointer to access them involves an extra level of indirection and hence an extra memory access to retrieve the address of the variable.

eg. int fred; int *p = &fred;

If you access fred, then the compiler already knows the address of fred and can directly read the value. If you access *p, then the compiler must first read the address of fred from the value stored in p. Then it can access fred as above.

[Well, that's somewhat simplified but I think it illustrates the point...]

In the above case, no there's no 'point' in using pointers.

Pointers do however have a use, even in "simple" applications. It all depends on what you need your code to do.

For example, suppose you have a number of similar global variables. Of these, one is 'active' at any one time, and which one is active is dependent on a number of variables and/or events. You may use a pointer to reference the 'active' variable, and update it when appropriate.

Typically pointers in C are used to increment through arrays, buffers, FIFOs etc, or to follow linked lists or similar data structures. They're also often used to pass large data structures (eg arrays) by reference to functions.

Umm, that's all that comes to mind atm...

Regards,

Mark McDougall, Engineer Virtual Logic Pty Ltd, 21-25 King St, Rockdale, 2216 Ph: +612-9599-3255 Fax: +612-9599-3266

In addition to Mark's excellent answer I can add the following real-world example in which a button on my GUI may display one of a number of different images on it depending on which language is selected.

The image data is stored in a large array which is part of a Bitmap structure. Rather than have to decide which language is in use and then which picture to draw every time the button is drawn, I have a pointer which points to whichever Bitmap structure is current. I then draw the button image pointed to which removes the need for a decision on every redraw. The only time the pointer is modified is when the language is changed.

Thanks for the help guys. Has cleared things up a bit. Cheers Rob

I think the day will come when they are so second-nature to you, you'll wonder how you ever did anything useful without them. I'm working on a demo project for my company that displays bitmaps on an LCD and I have "pointers to pointers to pointers" to deal with all the graphics that I have defined...

"extern const unsigned char ***maMenus[];"

...where a bitmap (representing a single ASCII character) is an array of unsigned chars, a string is an array of pointers to these bitmaps, a menu is an array of pointers to these strings, an "array of menus" is an array of pointers to these menus :-p

I'm sure I'm not alone either!!

R.

For embedded systems, one example might be:

if (serialport == 0) { SerialPort1.dataout = ch; SerialPort1.dataready = 1; } else if (serialport == 1) { SerialPort2.dataout = ch; SerialPort2.dataready = 1; } else if (serialport == 2) { SerialPort3.dataout = ch; SerialPort3.dataready = 1; } else { SerialPort4.dataout = ch; SerialPort4.dataready = 1; }

versus:

theSerialPort->dataout = ch; theSerialPort->dataready = 1;

-- Darin Johnson

I would look closely at the code produced using the different approaches. Intel caught me on that one with their 80C196. The IAR C compiler produce one line of code when assigning the address for Array[i] to the pointer p.

INT16 *p; // pointer to a 16 bit entity INT16 i; // a 16 bit signed variable INT16 Array[100]; // an arrat of 16 bit entities

i = 3; // Setting i to some value p = Array[i]; // setting pointer p to the ith element of Array

Typically that code would generate several assembly language instructions. So the penalities are CPU and compiler dependent for sure

George

Nah.

Declare a pointer to a structure, indexed into an array ( SerialPort1 through SerialPort4 as SerialPort[ 0 ] through SerialPort1[ 3 ]) via index serialport. Point to the appropriate structure in the array. Work uniquely via this pointer.

Rule# 1: never duplicate code that you can factor into a pointer.

Steve

formatting link

OTOH, this may have been your point. If so, I love you.

Steve

formatting link

But that doesn't work if the hardware registers don't fit nicely into an array, such as not all of them being at a multiple of the structure size. It does make the simplifying assumption that the registers are at least the same for the different ports, which isn't always true in real life.

All a pointer really is though is an index into a giant array of addressable units (ie, byte). So these are essentially the same concepts. At the assembly level, there also really isn't a concept of a "pointer to a variable". There is direct memory access and indirect memory access, which is sort of close to the distinction between a normal variable access versus a pointer access but not quite. A pointer is just an abstraction in that some programming languages use.

-- Darin Johnson

A pointer is a variable whose value tells your program which of several other variables to use at the moment. This isn't a capability you need all the time; I write long programs with no pointers in them; but when you need it, you need it.

Note that arrays are a special case of pointers. An array is a collection of variables such that the value of another variable (the subscript) can tell you which one of them to use.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required