I have a big question and it is: What are pointers and what are they for? , since I have been learning Java at university, but now we are teaching the basics of C. But the pointers issue has not been clear to me.
I would like someone to clarify it for me, and with small examples if possible.
Thank you in advance and greetings!
A pointer is an object that 'points' to another variable, usually using the address of the variable. In C, similar to other imperative languages, you can declare and define a variable in statements like these:
When the program is executed, the operating system grants it an appropriate memory region to store the code of the program in execution, as well as a data region to store the contents of the variables that the program uses and other regions that the program will use for its execution. correct execution. In the example above, the variables age and weight will be located in that data region.
You as a programmer have no control over the memory locations 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 weight variable is located at 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 they are done by modifying the values of the memory addresses, if in your program the following instruction is:
the compiler translates this statement as follows:
Therefore, the result is that in the memory address 1000 the content is no longer 3 but 4. With the above I try to explain that at a low level the machine does not understand variable names but memory addresses and operations that are done at 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 the 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:
The previous instruction indicates that it declares a pointer (by the asterisk) to a variable of type integer (int) called pointerAge, since this is also a variable, it will have a memory address in the data region:
The previous image shows that it is located at memory address 1008, as I mentioned, the content of this variable is the memory address of another integer type variable, since I want to store the address of the age variable (which I will only know in time). of execution), I indicate the following instruction:
The & operator in C returns the memory address where the variable is located. Therefore, the content of the data region is now:
Let's see what happened: I declared a variable called age and the memory address of this variable is 1000, because that's what the operating system decided. At the same time create a pointer variable called agePointer where I will store the memory address of the age variable, that is, the value of agePointer will be 1000.
Once this is done, I can modify the content of the age variable using the pointer, because I already know its memory address. I do this using the * dereference operator in C:
The above statement is equivalent to "find the memory address that stores the variable AgePointer and assign the value 8". So you will see that the variable stores memory address 1000, then it will go to memory address 1000 and the old value (3) will change it to 8.
Pointers are used as an indirect way to manipulate the content of other variables. The natural question is why not directly manipulate the variable instead of creating a pointer? i.e. what advantage is there in doing this
than in directly changing the value?
The answer lies in the very origin of C. C can be considered a high-level language, since it is a higher abstraction than an assembly language, but it can also be considered a medium-level language, since it offers in its very syntax the option of avoiding abstraction. closely. C was designed with the aim of offering a syntax closer to the programmer than assembly language, but without abandoning the possibility of directly manipulating memory addresses. This is so because it was designed to create operating systems. Manipulating arrays and data structures more complicated than just primitive variables (such as integers, floats...) is much easier using pointers than the variables themselves. Passing pointers as parameters to functions is much easier and faster than passing variables, because in the first we only pass memory addresses while in the second we pass the absolute copy of the variable. There are many more reasons to use pointers to manipulate variables, which you can find in thereference 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 (like int, double, boolean) and reference variables, or any user-defined data type instantiated via the new operator. But, unlike C, in Java I can't use pointer arithmetic. In C, pointer arithmetic consists of performing 'addition' or 'subtraction' operations on memory addresses, for example: obtain the memory address of the age variable, add 4 to the address and store the value 8 in that resulting address. It does not allow it for two reasons: first, because the Java virtual machine takes care of this in an absolutely transparent way for the programmer, and second, because in the design of the language they considered that it was unnecessary and insecure, and in reality, in C, handling pointers irresponsibly creates a lot of 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 aliases to memory regions where object properties are stored, similar to C, the exact representation of which depends on the Java virtual machine used.
The pointer is basically a variable that stores a memory address. And the content of this address will depend on the type of pointer. For example:
what I'm doing is telling it that the pointer is going to contain the memory address of an integer variable. I think I have been clear, otherwise I try to explain it in another way.