I understand what pointers are, but I don't understand why they are useful. I have read many tutorials about pointers, and yet I still don't know why someone would want to use one. Could anyone explain it to me? Or give me a link to something that explains it well? Thanks in advance for any help.

Wow, SpS, that's a long tutorial...

Good god that is long, but it looks useful thanks!

I don't know how to explain it the best... Look, there are variables, which in c terms are really called objects. We can consider these objects as folders, which contain something. Folder has two different properties, it usually has a name, and it has its physical location, somewhere on the shelf. Now we can give someone a copy of the folder, in which case he doesn't see the folder itself and may not know anything about the physical location of the folder. Or we can give him access to the folder, in which case he knows its physical location, can change its content, and can also rearrange the folders on the shelf. The folder name is the name of the object (variable), and the physical location of the folder is its reference, which is the value of the pointer (we may consider that the physical location of some folder may be written in another folder, which is pointer). By knowing the physical location of the objects, we can do many things. We may refer to these objects anywhere, giving access to them (enabling others to change them), and we can arrange the objects, physically or logically, any way, the simplest is the array, which in c can always be accessed by pointer (when array is a pointer, then array [n] is always the same as *(array + n)). More complex are the linked lists, binary trees etc, which enable some dynamic arrangement, ie we don't have to change the physical location of many objects, for any rearrangement. Pointers are often members of the structures, which makes the structures some kind of nodes, to build a bigger system, such structures can be arranged into arrays, or into more complex arrangements. There are also function pointers, which enable to refer to different functions as the need may be, instead of always using one fixed function, this also enables to create entities in c which are called objects in c++. Pointers also enable to allocate storage for objects -- when we allocate memory, we get the location of the beginning of the allocated storage area, where we can put diferent objects, this enables a dynamic memory management. I hope that i could somewhat explain why the pointers are necessary, but unfortunately i have not much idea how understandable it would be for someone who yet knows nothing about pointers.

Concerning the pointers tutorial, one of the classic ones is the ted jensen tutorial on pointers and arrays in c http://home.netcom.com/~tjensen/ptr/pointers.htm The code in the examples is not exactly the best possible, this is usually the case in any tutorial, though there are so many which are so much worse. But it is also a good tutorial because it's the only pointers tutorial i know, where the examples are in public domain. The programming examples and tutorials in general must be in the public domain, because one should be able to use the code from examples freely, without any license restrictions.

commented: Good link to the Jensen tutorial, which is one of the best +9

> I understand what pointers are, but I don't understand why they are useful.

>Good god that is long, but it looks useful thanks!

Many pages have been written in articles, tutorials and book chapters about pointers in C programming. A typical approach would be to explain how a variable is pass as a value in a function. meaning that a copy of the value is greated, and how a pointer could make that variable been pass as a reference.

All this is fine, except that explaining how a pointer is used doesn't necessarily brings light, about the value of using pointers; for those of us, that do not have enough experience with memory in programming with C.

Therefore, I would like to use an analogy drawn from day to day experience.
Think about a bank. You put money in a bank and when the time comes that you want to incurre an expense, you could withdraw the cash to pay for it. However, writing a check, paying with a credit card, using a money order or electronic transfer are more convinient.
Those instruments of payment are "pointers" to the real money, if we can ever call money real.

Suddenly, many more options are created. You don't have to go to the bank any more, you can make your employer to transfer the money electronically. You can manage that money without ever seen "green" again.
In fact without these "pointers" to real money, stock market would come to a crawl. Can you imagen a broker saying "I want to buy 2000 actions of Coca-cola", let me call the "moving guys to bring a couple sacks with bills"?. "Let me count it first, before I can sell them to you" says the other.
Instead they make electronic transactions to handle money without having to access the "real thing".

That's the value of using pointers to variables holding values in C programming.

Hi
This is Vineet Kumar.I really like System Programming and Pointers are the tool which make the simple programe to System level Programe.Actually Pointer is a variable which contain the address of another variable.It means one can capture the address of any variable and also the information which variaable have.Actually. after the compilation,the instruction set contain the pair of variable
and address.After decoding the instruction ,control unit examine the
operation which will be perform over the variable and for getting the variable put the address in the register.I think now u can get something,about the what powerful is pointer.


By...
Vineet Kumar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.