Register to reply

[C] Pointers seem useless

by Avichal
Tags: pointers, useless
Share this thread:
Avichal
#1
Nov9-12, 02:22 AM
P: 283
"Pointers are variables that store address of another variable"
But I don't understand why such a thing was necessary. The only thing they are useful for is when we want to permanently change something passed to a function.
Since a function in C creates a local copy it only changes the value of parameters temporarily. But if we pass pointers it changes the address and hence we can permanently alter the value of the data passed to a function.

Well this is pointless. Instead of having pointers to do this they could have had an option whether to create a local copy or pass the original value to a function. Why need pointers?

Maybe I'm missing something, help me realize some usefulness of pointers.
Also is the array system of C bad? I have heard that it doesn't handle arrays properly. Why is that?

Thank you!
Phys.Org News Partner Science news on Phys.org
Scientists develop 'electronic nose' for rapid detection of C. diff infection
Why plants in the office make us more productive
Tesla Motors dealing as states play factory poker
Borek
#2
Nov9-12, 02:46 AM
Admin
Borek's Avatar
P: 23,594
Quote Quote by Avichal View Post
Well this is pointless. Instead of having pointers to do this they could have had an option whether to create a local copy or pass the original value to a function. Why need pointers?
Original value is stored somewhere in memory - and the simplest way of telling the function where the original value is, is to pass it a memory address. And memory address is nothing else but a pointer.

But the main use of pointers is for dynamic memory allocation, when variables are created only when they are needed and in required size.
M Quack
#3
Nov9-12, 05:23 AM
P: 669
C does not perform any checks on array indices, for example.
According to who you talk to that is handling things "not properly".

It is common in C to navigate over arrays or strings using pointers. That can be very efficient, but the compiler does not perform *any* checks for you, so it can be quite dangerous if you don't do your own, explicit checks.

Many other languages use pointers, but generally in a much more restricted and often hidden way.

C is a very minimalist language dating back to when CPUs were slow and memories small. The original K&R C was even more minimalist than the ANSI C that is used today. When writing C code you have a fairly good idea of *everything* the compiler is going to do, there is almost nothing going on below the surface.

rcgldr
#4
Nov9-12, 06:44 AM
HW Helper
P: 7,132
[C] Pointers seem useless

Pointers are used in a lot of situation. As already mentioned, pointers are useful for dynamically allocated objects. Linked lists or tree like structures use pointers. An array of pointers to strings is how C passes a variable number of command line arguments to main(int argc, char*argv[]) (or main(... , char **argv)), via argv. An array of pointers to records in memory is useful for sorting since the sort can just sort the array of pointers, then do one pass using the sorted array of pointers to move or output sorted records.

Pointers to functions are also useful in some situations. A command driven program, perhaps interactive with the user, can use an array of structures, where each structure has a command string (or pointer to command string) and a pointer to function to handle that command. Optional command parameters could be handled in the same way that main() handles command line parameters with a count and an array or pointers to parameter strings.

Pointers to functions can also be used to save the current state in an interrupt or event driven process where the code is externally called at each interrupt or each event (note that a typical windows program is event driven where each event is associated with a message to be handled). For example, the function pointer for a specific event type would be initialized to functionA(). When the interrupt or event occurs and functionA gets called, it can process the interrupt or event step, update the function pointer to functionB(), and return. At the next interrupt or event, functionB() can update the function pointer to functionC(), and so on. This interrupt or event driven process can go through a series of functions until the current event sequence is completed and the function pointer reset back to functionA() or to a function to handle the case where the specific interrupt or event should not occur.

Quote Quote by Avichal View Post
Instead of having pointers to do this they could have had an option whether to create a local copy or pass the original value to a function. Why need pointers?
C++ implements something similar to this by allowing a function to optionally choose to handle a paramter as pass by value or pass by reference (which is actually a pointer, but the syntax will be the same as if the parameter is pass by value). One caveat with this is that the calling code uses the call by value syntax in both cases, and unless the function or it's prototype is examined, there's no way to know how the function is handling it's parameters (by value or by reference).
Avichal
#5
Nov9-12, 10:07 AM
P: 283
Just deviating a little. In linked list we use pointers to point to the next data
I have been taught like this: -
typedef struct node
{
        int data;
        struct node *next;
}node;
In this we have a pointer to the next node. Is it necessary to have a pointer? I mean, it stores the "address" of the next node. Instead why can't we just store the "value" of the next node. Why store the address and then dereference it whereas instead we could directly store the value?
jtbell
#6
Nov9-12, 10:15 AM
Mentor
jtbell's Avatar
P: 11,782
Quote Quote by Avichal View Post
why can't we just store the "value" of the next node
The next node would in turn have to contain the "value" of its successor node, and so on, until the end of the list. You would have a set of nested structs, one "inside" the other. How would the compiler allocate memory for such a thing, without knowing in advance how many levels of nesting there are (i.e. the size of the list)?
Avichal
#7
Nov9-12, 10:23 AM
P: 283
Quote Quote by jtbell View Post
The next node would in turn have to contain the "value" of its successor node, and so on, until the end of the list. You would have a set of nested structs, one "inside" the other. How would the compiler allocate memory for such a thing, without knowing in advance how many levels of nesting there are (i.e. the size of the list)?
How would pointers solve this problem? If we use pointers then we would have nested structs pointing to the next node indefinitely.
Borek
#8
Nov9-12, 11:32 AM
Admin
Borek's Avatar
P: 23,594
Quote Quote by Avichal View Post
How would pointers solve this problem? If we use pointers then we would have nested structs pointing to the next node indefinitely.
No, they are not nested. What you suggested was a drawer inside a drawer inside a drawer inside a drawer and so on. Using pointers each drawer contains just a piece of paper with the NUMBER of the next drawer.
Avichal
#9
Nov9-12, 11:46 AM
P: 283
Oh the drawer analogy helped a lot. So does every language has the concept of pointers as now I understand that they are pretty useful
jtbell
#10
Nov9-12, 11:48 AM
Mentor
jtbell's Avatar
P: 11,782
Also, with pointers you make new drawers one-by-one on an as-needed basis, and dispose of them one-by-one as they are no longer needed, instead of having to make a complete nested contraption at once (which means you have to know in advance how many drawers you will need).
rcgldr
#11
Nov9-12, 01:32 PM
HW Helper
P: 7,132
Quote Quote by Avichal View Post
In linked list ... taught like this:
typedef struct node
{
        int data;
        struct node *next;
}node;
It's usually better to have the next pointer first, which would allow you to link different types of structures on the same list (such as a message queue). Also you used "node" for both the struct name and the typedef name, which could be an issue depending on the compiler. Microsoft's "standard" is to usually prefix struct names with underscore:

typedef struct _node
{
    struct _node *next;
    int data;
}node;
With C++, inheritance could be used to put multiple class types on a single list:

class Node{                             // base node class
public:
    Node * next;
    Node(){next = NULL;}
};

class NodeInt : public Node{            // inherited node class with int
public:
    int value;
    NodeInt(int v) : value(v) {next = NULL;};
};
Yuu Suzumi
#12
Nov14-12, 10:27 AM
Yuu Suzumi's Avatar
P: 16
A very paedagocical example is given here:
http://stackoverflow.com/questions/5...-in-programing

I am not copy-pasting it in order not to take credit for other peoples explanatory skill.


Register to reply

Related Discussions
Is chemistry useless? General Discussion 29
Is everything we're doing wrong or useless? General Discussion 25
Useless Trivia General Discussion 78
Useless Charities General Discussion 8