Why do you need pointers in C?

I've already watched a few videos on the subject and now have a rough idea of ​​how to use it, but I still don't understand what I need it for. When can I use a pointer, or when does it make sense or simplify something? A concrete example would be helpful. For me, a pointer is nothing more than a type of variable that accesses a memory address. But what do I want to do with the memory address?

(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
7 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
L4ze3
1 year ago

As Kelec has already said, pointers are used first of all when implementing Pass by Reference. Here is a practical example for using pointers if you want to swap the value of two variables

#include 

void swap(int *a, int *b)
{
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
}

int main()
{
    int num1 = 69;
    int num2 = 420;

    /* num1 = 69, num2 = 420 */
    printf("Bevor dem Tausch: num1 = %d, num2 = %d\n", num1, num2);


    /* Hier werden die Speicheradressen der Variablen übergeben */
    swap(&num1, &num2) // pass by reference

    /* num1 = 420, num2 = 69 */
    printf("Nach dem Tausch: num1 = %d, num2 = %d\n", num1, num2);

Since the memory addresses are transferred here, the variables are changed since the memory is accessed directly and the value of the variables is not copied, as in a function that does not use pointers.

#include 

void swap(int a, int b)
{
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
}

int main()
{
    int num1 = 69;
    int num2 = 420;

    /* num1 = 69, num2 = 420 */
    printf("Bevor dem Tausch: num1 = %d, num2 = %d\n", num1, num2);


    /* Hier werden nur die Werte der Variablen übergeben */
    swap(num1, num2) // pass by value

    /* num1 = 69, num2 = 420 */
    printf("Nach dem Tausch: num1 = %d, num2 = %d\n", num1, num2);

Nothing would happen here, since the value of num1 and num2 is only copied, so the variables do not change, but only the copied values within the function

Kelec
1 year ago

Pointers are mainly used to realize so-called Pass by Reference function calls.

Let’s say we have a memory type called mytype_t then this would be a function:

void function(mytype_t* t);

The call would then be:

mytype_t t;
function(&t);

There are then essentially 3 reasons why you do that.

Number 1 is speed:

In a function call, the variable is always placed on the stack and then removed from the stack. If mytype_t is a very large data type then it is more efficient to write and read the memory address on the stack instead of the complete variable. From then on, this brings you something from when the data type is larger than a pointer so on a 64-bit system it will bring something if mytype_t is greater than 64-bit.

Number 2 the function wants to change something to the variable:

If we now assume the function function changes any values in t, then this change would not apply outside the function when a normal function call (pass by copy). If you pass a pointer, they do.

This then leads directly to point 3 Opaque Handle:

An Opaque Handle is a way and wise how to use C++ objects in C and it is a way to conceal the inner implementation of a library. The Opaque Handle is a pointer about which the program only knows that it is a pointer, but the program doesn’t know what’s right in this pointer.

The handle serves only the library as an instance and contains all the essential information of the library such as settings etc. However, since the C program does not know anything about it, it can only affect the features of this handle by means of functions and so that these changes really get through, the Opaque Handle must be a pointer.

Now, considering that “this” in C++ (the instance variable) is also just a pointer, it becomes clear how a C++ C interface looks. It is now possible to define C functions which use the instance variable as an opaque handle and thus extend the C++ instance to C and that even though C does not even know that such a thing as classes and instances exist at all.

KarlRanseierIII
1 year ago

Kelec has already submitted a good answer, I would like to join it and throw ‘Generics’ into the room, on an example from libc:

void qsort(void base[.size * .nmemb], size_t nmemb,
                     size_t size, int (*compar)(const void [.size], const void [.size]));

qsort() assorted arrays and to be able to perform this task generically, it must know at what address the array is, how many elements it is long, how large the individual element is and must be able to compare the elements.

The base address is a pointer, the comparison function is also determined via its address, wherein this in turn receives the generic addresses of two elements, which again requires pointers.

A second point I have hitherto lacked is dynamic data structures. The pointer is a form of the reference type and many data structures such as graphs (spaces), lists, … use ‘avoid’ within the data structure, i.e. references to another element.

NorbertWillhelm
1 year ago

Zero-terminated character strings are also fields or hands.

You also need pointers to use a C-API, like the WinApi.

Functional pointers, such as GetProcAdress, are also pointers.

CSANecromancer
1 year ago

But what do I want with the memory address?

The memory address will tell you where certain data are or certain program parts (methods, functions) begin.

Franz1957
1 year ago

Pointers are useful to build linked data structures. Examples are lists and trees. KarlRanseierIII has already pointed out.

https://de.wikipedia.org/wiki/Liste_(Data structure)

https://de.wikipedia.org/wiki/Baum_(Data structure)

https://de.wikibooks.org/wiki/Algorithms_and_Data Structures_in_C/_Listen

https://de.wikibooks.org/wiki/Algorithms_and_Data Structures_in_C/_Binarian_Bäume

http://www.edm2.com/0505/introc9.html

http://www.edm2.com/0506/introc10.html