What exactly are pointers in C?

I have a big doubt and it is: What are pointers and what are they for?, since I have been learning Java in college, but now we are giving the basics of C. But I have not been clear about the topic of pointers.

I would like someone to clarify it to me, and with small examples if possible.

Thank you very much in advance , and greetings!

 9
Author: Juan Pinzón, 2016-04-22

2 answers

A pointer is an object that 'points' to another variable, usually using the same address. In C, similar to other imperative languages, you can declare and define a variable in statements like these:

int edad = 3;
double peso = 4.4;

When running the program, the operating system gives it an appropriate memory region to store the code of the running program, as well as a data region to store the contents of the variables that the program uses and others regions that the program will use for its proper execution. In the example above, the age and weight variables will be located in that data Region.

Program Variables

You as a programmer have no control over the memory places where these variables are created, this depends on both the operating system, and the compiler implementation. The image above shows a possible assignment: the age variable is located at memory address number 1000 and contains the value 3, the variable weight is located in the memory address number 1004 and contains the value 4.4. The names of the variables are not shown in the graph because when they are executed they lose their meaning: at a very low level there are no variable names, but memory addresses, all the operations that you do with these variables at the hardware level are operations that are done by modifying the values of the memory addresses, if in your program instruction is:

edad = edad + 1

The compiler translates this statement as follows:

  1. search the program data region for the address of the age variable.
  2. get the value that memory address has.
  3. to that value add 1.
  4. save that result to the memory address of the age variable.

So the result is that in the memory address 1000 the content is no longer 3 but 4. With the previous I try to explain that at a low level the machine does not understand variable names but memory addresses and operations that are done to those memory addresses. The name of the variables is only useful for the programmer, precisely to abstract these details that are dependent on the operating system and the compiler.

Here variables known as pointers come into play, a pointer is a variable that stores the memory address of another variable. In C, you declare a pointer like this:

int* apuntadorEdad;

The above statement indicates that it declares a pointer (by the asterisk) to an integer type variable (int) which is called pointer, as this is also a variable, it will have a memory address in the data Region:

Pointer

The above image shows that it is located at memory address 1008, as I mentioned, the content of this variable is the memory address of another variable of integer type, since I want to save the address of the age variable (which I will only know at runtime), I give it the following statement:

apuntadorEdad = &edad;

The & operator in C returns the memory address where the variable is located. So the content of the data region is now:

Pointer

Let's see what happened: I declared a variable called age and the memory address of this variable is 1000, because so decided by the operating system. Create at the same time a pointing variable called pointing where I will save the memory address of the age variable, i.e. the pointing value will be 1000.

Once this is done I can modify the contents of the age variable using the pointer, because I already know its memory address. I perform this using the * dereference operator in C:

*apuntadorEdad = 8;

The above statement is equivalent to " find the memory address that stores the variable pointing and assign it the value 8". So you will see that the variable stores the memory address 1000, then it will go to the memory address 1000 and the old value (3) will change it to 8.

Pointer

Pointers are used as an indirect way of manipulating the contents of other variables. The natural question is why not manipulate the variable directly instead of creating a pointer? i.e. what advantage is there in doing this

int valor = 123; //Supongamos que tiene dirección de memoria 1000
int* apuntadorValor = &valor; //Ahora el contenido de apuntadorValor es 1000
*apuntadorValor = 456; //Ahora el contenido de valor es 456

That in directly change the value?

valor = 456;

The answer is in the same origin of C. C. It can be considered a high-level language, since it is a greater abstraction than an assembly language, but also a medium-level language because it offers in its same syntax the option of avoiding abstraction in a close way. C was designed with the aim of offering a syntax closer to the programmer than the assembly language but without abandoning the possibility of manipulating directly memory addresses. This is so because it was designed to create operating systems. Manipulate more complicated arrays and data structures than just primitive variables (such as integers, floats...) it is much simpler by using pointers than the same variables. Passing pointers as parameters to functions is much simpler and faster than passing variables, because in the first we only pass memory addresses while in the second we pass the copy absolute of the variable. There are many more reasons to use pointers to manipulate variables, which you can find in the reference book par excellence of the C language.

¿are there pointers in Java?

Strictly speaking, yes: in Java you have two types of variables, primitive variables (such as int, double, boolean) and reference variables, or any type of data defined by the user and instantiated through the new operator. But, unlike from C, in Java I can't use pointer arithmetic. In C the pointer arithmetic consists of performing operations of 'addition' or 'subtraction' of memory addresses, for example: obtain the memory address of the age variable, add 4 to the address and in that resulting address store the value 8. It does not allow it for two reasons: first because the Java Virtual Machine takes care of this absolutely transparently to the programmer and second because in the language design they considered it unnecessary and unsafe, and is that in reality, in C, the handling of pointers irresponsibly generates many headaches. Doing pointer arithmetic could access memory addresses to which the program does not have access permissions and corrupt data that the operating system requires. Reference variables in Java are actually an alias to memory regions where object properties are stored, similar to C, whose exact rendering depends on the Java virtual machine used.

 14
Author: dwarandae, 2016-04-23 06:16:46

The Pointer is basically a variable that stores a memory address. And the content of this address is going to depend on the type of pointer. For example:

Int * pVar;

What I'm doing is telling it that the Pointer is going to contain the memory address of an integer variable. I think I was clear, but I'm trying to explain it another way.

 2
Author: Matias R., 2016-04-23 02:08:55