58
P O I N T E R S: The Basics

P O I N T E R S: The Basics

5 years ago
Anonymous $Dftgs0JzgE

https://medium.com/@goutiemma/p-o-i-n-t-e-r-s-the-basics-afeb2b173d21

Pointers are one of the most powerful fundamentals of the C/C++ language, because developers are given direct access to the memory from one’s code in an easy and fast way. Pointers are variables whose value is the address of another variable, i.e., direct address of the memory location. So, in a sense pointers are a symbolic representation of addresses. They enable programs to simulate pass by reference, or pass by address, and can create and manipulate dynamic data structures. In other words, pointers allocate memory dynamically, which means memory allocated “on the fly” during run time that is usually placed in a program segment known as the heap or the free store. And when variables are passed by reference, the actual address is passed. If you’re still confused with the difference between pass by value and pass by reference, think of it like this: in the case of call by value we pass copies of values of passing arguments, in case of pass by reference, the address of the values is passed and no copies of the actual values are made. The important thing to take away is that, if you pass by reference, any manipulation of the data is reflected throughout. This means that if the value of a parameter changes in a function, that change will be reflected in the actual variable that you passed during the function call. However, the same is not true for pass by value. The changes are not reflected in the actual parameters.

To declare a pointer variable, you need to specify the data type of the value in the memory location that pointer variable points to. The syntax will look something like datatype identifier;, with the asterisk symbol being placed anywhere between the two. For convention, the asterisk is typically attached to the variable name, like this: int ptr or char *ch. All of the examples below, however, are equivalent.