Why Are Pointers Necessary in Programming?

  • Thread starter Avichal
  • Start date
  • Tags
    Pointers
In summary, pointers are variables in C that store the address of another variable. They are useful for permanently changing the value of a parameter passed to a function, as it allows for the address of the original value to be passed. Pointers are also commonly used in dynamic memory allocation and navigating over arrays or strings. However, they can be dangerous if proper checks are not performed. C does not handle arrays properly and relies on the programmer to perform checks. Pointers to functions can also be used for command-driven programs and interrupt/event-driven processes. In linked lists, pointers are necessary as they allow for the next node's address to be stored, rather than the value itself. This allows for a dynamic and efficient allocation of memory for a potentially infinite
  • #1
Avichal
295
0
"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!
 
Technology news on Phys.org
  • #2
Avichal said:
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.
 
  • #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.
 
  • #4
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.

Avichal said:
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).
 
Last edited:
  • #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;
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?
 
  • #6
Avichal said:
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)?
 
  • #7
jtbell said:
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.
 
  • #8
Avichal said:
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.
 
  • #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
 
  • #10
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).
 
  • #11
Avichal said:
In linked list ... taught like this:

Code:
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:

Code:
typedef struct _node
{
    struct _node *next;
    int data;
}node;

With C++, inheritance could be used to put multiple class types on a single list:

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;};
};
 
Last edited:

What are pointers in C and why do they seem useless?

Pointers in C are variables that store the memory address of another variable. They may seem useless because they add an extra level of complexity to programming and can lead to errors if not used correctly.

Why are pointers important in C?

Pointers are important in C because they allow for more efficient memory management and can lead to more efficient code. They also enable the creation of data structures such as linked lists and trees, which are essential in many applications.

How do pointers work in C?

Pointers work by storing the memory address of a variable. This address can then be accessed and manipulated to modify the value of the variable or to point to a different location in memory.

What are some common errors associated with pointers in C?

Some common errors associated with pointers in C include dereferencing a null pointer, accessing memory that has not been allocated, and creating memory leaks by not properly freeing allocated memory.

Are there any alternatives to using pointers in C?

Yes, there are alternative ways to achieve similar results without using pointers in C. These include passing variables by value instead of by reference, using arrays instead of pointers, and using higher-level languages that handle memory management for you.

Similar threads

  • Programming and Computer Science
Replies
2
Views
896
  • Programming and Computer Science
Replies
5
Views
846
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
5
Views
759
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top