POINTERS in C What for ?

Hi all

what is the general idea behind pointers in C ?

Why not just assign VARs and manuipulating them and asigning thier content to wherver needed ?

Looks to me that pointers are jusy a head ake for orgrammers .

Thanks Elico

Pointers are primarily for dynamic memory allocation, though there are a variety of other uses for them as well.

A headache yes, but only when you get them wrong.

Arrays in C are just an alias of pointers, so if you think naming 100 (or a thousand, or...) variables is an easier way of manipulating and keeping track of them, versus simply creating an array of them, then go ahead.

Pointers are very usefull if you need to handle larger data objects, like structs, arrays or strings. If such an object is larger than - let's say - 10 bytes, then it is better not to copy all the 10 bytes, but pass a pointer to that object instead.

The concept of "pass by reference" is implemented with pointers in C and C++.

Oliver

I doubt I could write more than 10 lines of C code without using a pointer or two :)

You can think of them as helping to make you code object oriented, for example you have a generic routine that performs some work and you pass a pointer (the object) to it.

Also I think they are faster than indexing into an array although I've never looked at the compiled code to verify that.


Rob

elico: what is the general idea behind pointers in C ?

Well, why not do some research?

A tutorial on pointers and arrays in C

A Google search for "C pointers" reveals 38,400,000 page hits, so this is hardly an obscure subject.