Introduction to pointers

From Applied Science
Revision as of 20:23, 19 April 2022 by Wikiadmin (talk | contribs) (Created page with "In the simplest exercises and examples, about conditional structures, loops, arrays and matrices and functions, a pointer was not required to solve the problem. In some occasions the pointer showed up, like in the case of scanf() function, but the details about how it works were omitted to simplify and maker it easier to understand. Can a function calculate two values and return both? Calculate two different values yes, but return both no. That's when we need pointers....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In the simplest exercises and examples, about conditional structures, loops, arrays and matrices and functions, a pointer was not required to solve the problem. In some occasions the pointer showed up, like in the case of scanf() function, but the details about how it works were omitted to simplify and maker it easier to understand. Can a function calculate two values and return both? Calculate two different values yes, but return both no. That's when we need pointers.

Pointers are a pretty abstract concept. It's recommended that a lot of problems that require pointers are solved, otherwise it becomes much difficult to understand what are and how to use pointers. Mentally, we store, retrieve and change memories without executing any mental operation that tells us where a memory is (at least not consciously). In the computer, the location of memories is exposed trough pointers. Else how would the computer work?

It's not possible to make an analogy between functions of 'void' type and mathematics, because there is no function in math that doesn't calculate a final value. 'void' functions are better understood as "recipes", operations to perform something that isn't necessarily a final value or one outcome. For example: sort numbers in ascending order, for different inputs there are different outputs, just the "recipe" about how to sort in ascending order remains fixed in the function.

Depending on the class and the teacher, pointers are studied too briefly or even disregarded at all in the course. Which is not good.

The basic logic is: pointers are a variable type that stores memory addresses. They are a variable that "mediate" certain operations. Pointers are intimately linked to functions.

Errors of logic:

   Pointers that point to unknown or incorrect addresses;
   A good portion of the syntax errors does not allow a program to compile.