Tux

...making Linux just a little more fun!

C programming - when to use pointers

David Chanters [david.chanters at googlemail.com]


Sat, 26 May 2007 14:32:34 +0100

Hey all,

I'm fairly new to C programming but am familiar with most programming principles from Java and Perl. I understand what pointers are in C, but I have lots of problems knowing when best to use them, since many, if not all tasks, can be obtained just from passing parameters into function directly.

Are there any good tips for knowing when or when not to use pointers?

David.


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Sat, 26 May 2007 19:22:59 +0530

Hi David,

Welcome to the world of C Pointers!

Traditionally, pointers is such a C feature which is often either under-used or over-used in C programming. These points may be worth noting

1. In some cases pointers are just difficult/often complex alternatives for simpler things - like in case of arrays.

2. The other extremist view is that pointers are often indispensable - like linked lists, or places where we need dynamic memory allocation.

In C you have you have two ways of doing most things - using pointers or without it. For eg. you can write a Binary Search tree using arrays as well as pointers. But generally , you will need to use pointers in cases where you need some kind of dynamic memory allocation among others.

Also you must have already used the scanf() function, Now in due course of time, you will know that scanf() implicitly uses pointers.

There is an excellent tutorial here http://cslibrary.stanford.edu/102/

hope this helped you!

Cheers,

-- 
Amit Kumar Saha
http://amitsaha.in.googlepages.com

Top    Back


Ben Okopnik [ben at linuxgazette.net]


Sat, 26 May 2007 10:17:39 -0400

On Sat, May 26, 2007 at 02:32:34PM +0100, David Chanters wrote:

> Hey all,
> 
> I'm fairly new to C programming but am familiar with most programming
> principles from Java and Perl. I understand what pointers are in C,
> but I have lots of problems knowing when best to use them, since many,
> if not all tasks, can be obtained just from passing parameters into
> function directly.
> 
> Are there any good tips for knowing when or when not to use pointers?

If you're familiar with references in Perl, many of the same reasons apply. How would you pass multiple complex structures into a subroutine without them? How would you create complex structures (say, an array of arrays) without them? If you have a data structure that takes a significant portion of your memory, doing a "pass-by-value" (i.e., "passing parameters [...] directly") would amount to making a copy of the original structure - and thus consuming twice as much memory. Passing a pointer is faster and more efficient.

That being said - references in Perl (Ruby, etc.) are pleasant, polite, and bring a bottle of wine to dinner without being asked, while pointers in C are a pure, unmitigated evil and will perform unspeakable acts on your pet weasel. I'm just sayin'. :)

#!/usr/bin/perl -w
 
# Creates a hash (%employee_db) of employee IDs, which point to a list
# of hashes keyed by category (i.e., 'phone_nums'), etc. This defines
# the second cell number for employee 12345.
$employee_db{12345}{phone_nums}{cell}->[2] = '1-800-555-1212';
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *

Top    Back


René Pfeiffer [lynx at luchs.at]


Sat, 26 May 2007 18:27:46 +0200

On May 26, 2007 at 1017 -0400, Ben Okopnik appeared and said:

> On Sat, May 26, 2007 at 02:32:34PM +0100, David Chanters wrote:
> > [...]
> > Are there any good tips for knowing when or when not to use pointers?
>=20
> [...]
> That being said - references in Perl (Ruby, etc.) are pleasant, polite,
> and bring a bottle of wine to dinner without being asked, while pointers
> in C are a pure, unmitigated evil and will perform unspeakable acts on
> your pet weasel. I'm just sayin'. :)

If you are interested in a deeper insight into C pitfalls and tricks you should take a look at the book "Expert C Programming: Deep C Secrets". It has a lot of examples and is written in an entertaining style.

http://arstechnica.com/etc/books/deep-c.html

Best, René.


Top    Back