In practice, what is the use of C-pointers?

Asked

Viewed 4,426 times

21

Recently I have been studying the language and so far I have not identified practical utility for the use of pointers. I understand how it works very well, but nothing more.

3 answers

23


Essentially serves to create indirect. What can be very important to solve various computing problems, as the answer linked.

So instead of accessing a value directly, you have an address where you have that value. The value can be a lot, can even not be identified what is beforehand, which creates flexibility, and a certain insecurity, of course.

Array

One of the most common uses is to indicate where there is a certain information in a sequence, that is, a way to create something as a array. So the content of this array in practice is a memory address, ie is a way to point to where is the value you want. A a[2] for example, is where is starting the array, address stored in a and 2 is the displacement you need to make from that point, so the address you actually access is a + (2 * sizeof(int)) if that guy array for a int.

The very string is a array of characters. Then the type of a string is char *, or a pointer to characters.

Heap

There are several cases that allocate memory in stack does not meet the needs (it is not the case to mention here) and after allocating in heap need to have a way to access this object, the way to do this is to use a pointer to that object.

In practice there are opaque pointers even for the stack, is that you do not need to deal with them in your code saved in specific situations.

Parameter

Another great use is to pass an argument to a function when in fact you don’t want to copy the data, either because it is big or because what is changed in it should reflect in the original location, so instead of passing the data you pass the address where it is, that is a pointer. It’s often called go by reference, because you pass something that refers to the real object and not a copy of the object that would be normal by default (reference X pointer).

Anonymous function

The value of a pointer can be the code address of a function, so you can change the function to be executed based on the value of the pointer, that is, you can anonymize the function.

Incidentally, the address of a function does not stop being a pointer, but in C does not appear in the code. In practice the function as we use exists only because there is the pointer.

Array heterogeneous

May have a pointer. Along with the void * can solve a problem of array heterogeneous. When do not know the size of each element of the array and it can vary, creates a array of pointers that will point to the elements elsewhere, thus maintaining homogeneity of size of each element required by array.

It doesn’t have to be just one array. Any structure that needs to know the size and you don’t know until you use it. You delegate the object that doesn’t know the size to another place, then its structure that needs to know the size can have the fixed size because the variable object came out of there and only has a pointer to that object and the pointer has known size.

In practice only array may have its size not known by the compiler, any other structure than a array, one way or another, needs the known size.

I array does not need to know the total size, but the size of the element needs, after all it accesses the element with complexity O(1), therefore constant, so it has to be accessed with pure mathematics and this is only possible if the size of all elements is the memso.

Other

From there one can compose the pointer in several ways and obtain much more sophisticated mechanisms.

For example, in higher-level object-oriented languages pointer to the object. And also to be able to choose which method to use based on the real object (polymorphism). All this can be done in C, but you have to do in hand.

The fact that some languages do not allow the programmer to access a pointer does not mean that it does not have it, it is there hidden from you. C only ever exposes it (or almost).

An example of data structure that is only feasible with pointer is the linked list. The same goes for a tree. The only way to indicate where the following element is with a pointer.

In addition to examples of trivial programs the pointer is always used in codes.

There are operators for get an address of an object and to take the value of the referenced address.

See more:

7

Pointers exist in any language, the difference is that in C they are explicit, and in other languages they are usually not explicit.

When you declare an object in Javascript for example you are using a pointer:

var obj = { "nome": "Thiago" };

If you pass obj to a function, and the function changes some property of obj, the original object is changed. This is because obj is passed "by reference" to the function, and reference is nothing more than a pointer.

In C you can pass the pointer to a struct, which allows a function to change the values of the struct, but you can also pass the struct "by copy" which does not involve pointers, but entails literally copying the entire memory content of the struct. When you pass a struct "per copy" to a function, the original struct is preserved, i.e., changing the values of the struct in the function will not affect the values of the original struct.

Most languages only support passing values "per copy" to primitive types (int, Boolean, float, etc.).

  • 3

    Typically, languages that do not support pointers use reference. The difference between pointer and reference are the operations that each data type supports, with pointer usually having pointer arithmetic and any variable having the possibility to pick up its "corresponding pointer" (&var in C). References usually do not have these operations. Another thing, in Bash, reference is obtained using variable expansion ${!var} to pick up content stored in the variable that has name stored in $var, but it is not easy to return the value per parameter passed by reference

  • 1

    True, Jefferson, I’ve oversimplified.

  • @Thiagobarcala can give an edited answer if you think you have too simplified

0

In C, you don’t have support for some complex data types, such as a string. There is also no way to pass a variable "by reference" to a function. That’s where you have to use pointers. In addition, you can make them indicate virtually anything, chained lists, struct and so on.

Pointers should be used where there is no other option, for lack of proper functionality, or only for performance reasons, if you judge this to be a determining factor in your application, as you have direct access to computer memory.

  • 1

    Do not you consider complex structures? And the complex numbers? , predicted in the standard library?

  • 1

    It missed "some" in that paragraph, and ended up decontextualizing, I’ll correct, thanks for the touch.

  • 1

    I have some disagreements about the use of pointers, but my disagreements do not invalidate your answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.