| New Reply |
[C] Pointers seem useless |
Share Thread | Thread Tools |
| Nov9-12, 02:22 AM | #1 |
|
|
[C] Pointers seem useless
"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! |
| PhysOrg.com |
science news on PhysOrg.com >> Hong Kong launches first electric taxis >> Morocco to harness the wind in energy hunt >> Galaxy's Ring of Fire |
| Nov9-12, 02:46 AM | #2 |
|
Admin
|
But the main use of pointers is for dynamic memory allocation, when variables are created only when they are needed and in required size. |
| Nov9-12, 05:23 AM | #3 |
|
|
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. |
| Nov9-12, 06:44 AM | #4 |
|
Recognitions:
|
[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. |
| Nov9-12, 10:07 AM | #5 |
|
|
Just deviating a little. In linked list we use pointers to point to the next data
I have been taught like this: - Code:
typedef struct node
{
int data;
struct node *next;
}node;
|
| Nov9-12, 10:15 AM | #6 |
|
Mentor
|
|
| Nov9-12, 10:23 AM | #7 |
|
|
|
| Nov9-12, 11:32 AM | #8 |
|
Admin
|
|
| Nov9-12, 11:46 AM | #9 |
|
|
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
|
| Nov9-12, 11:48 AM | #10 |
|
Mentor
|
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).
|
| Nov9-12, 01:32 PM | #11 |
|
Recognitions:
|
Code:
typedef struct node
{
int data;
struct node *next;
}node;
Code:
typedef struct _node
{
struct _node *next;
int data;
}node;
Code:
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;};
};
|
| Nov14-12, 10:27 AM | #12 |
|
|
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. |
| New Reply |
| Thread Tools | |
Similar Threads for: [C] Pointers seem useless
|
||||
| Thread | Forum | Replies | ||
| useless | General Discussion | 1 | ||
| 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 | ||