



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
computer programming note about pointers
Typology: Summaries
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















We have already seen how variables are memory cells that we can access by an identifier. But these variables are stored in concrete places of the computer memory. For our programs, the computer memory is only a succession of 1
address.
The only
There are two pointer operators in C++: o & the address of operator o * the dereference operator the value of the variable pointed by this pointer will be returned Whenever you see the & used with pointers, think of the words “address of.” The & operator always produces the memory address of whatever it precedes. The * operator, when used with pointers, either declares a
pointer or dereferences the pointer’s value. The dereference operator can be literally translated to "value pointed by". A pointer is simply the address of an object in memory. Generally, objects can be accessed in two ways: directly by their symbolic name, or indirectly through a pointer. The act of getting to an object via a pointer to it, is called dereferencing the pointer. Pointer variables are defined to point to objects of a specific type so that when the pointer is dereferenced, a typed object is obtained. At the moment in which we declare a variable this one must be stored in a concrete location in this succession of cells (the memory). We generally do not decide where the variable is to be placed - fortunately that is something automatically done by the compiler and the operating system on runtime, but once the operating system has assigned an address there are some cases in which we may be interested in knowing where the variable is stored.
We have assigned to x the content of variable var as we have done in many other occasions in previous sections, but to ptr we have assigned the address in memory where the operating system stores the value of var , that we have imagined that it is 1776 (it can be any address). The reason is that in the
character. The variable that stores the address of another variable (like ptr in the previous example) is what we call a pointer. 4.1. Declaring Pointers: Is reserving a memory location for a pointer variable in the heap. Syntax: o data type * pointer_name ; to declare a pointer variable called p_age, do the following:
Whenever the dereference operator, *, appears in a variable declariation, the variable being declared is always a pointer variable. 4.2. Assigning values to pointers: p_age is an integer pointer. The type of a pointer is very important. p_age can point only to integer values, never to floating-point or other types.
To assign p_age the address of a variable, do the following:
Both ways are possible. If you wanted to print the value of age, do the following:
Or by using pointers you can do it as follows
o The dereference operator produces a value that tells the pointer where to point. Without the *, (i.e cout<<p_age), a cout statement would print an address (the address of age). With the *, the cout prints the value at that address. You can assign a different value to age with the following statement: o age = 13; //assigns a new value to variable age o *p_age = 13 //assigns 13 as a value to the memory p_age points at. N.B: the * appears before a pointer variable in only two places: when you declare a pointer variable and when you dereference a pointer
Note that we can’t assign the address of a float type variable to an integer pointer variable and similarly the address of an integer variable cannot be stored in a float or character pointer.
That means, if a variable type and pointer to type is same, then only we can assign the address of variable to pointer variable. And if both are different type then we can’t assign the address of variable to pointer variable but this is also possible in C++ by declaring pointer variable as a void as follows:
Let us see an example:
The difficulty on void pointers is that, void pointers cannot be de referenced.
They are aimed only to store address and the dereference operator is not allowed for void pointers. Let us see an example:
4.4. Arrays of Pointers If you have to reserve many pointers for many different values, you might want to declare an array of pointers. The following reserves an array of 10 integer pointer variables:
(^) The above statement will create the following structure in RAM
the following allocation would be valid:
At this point p and numbers are equivalent and they have the same properties, with the only difference that we could assign another value to the pointer p whereas numbers will always point to the first of the 20 integer numbers of type int with which it was defined. So, unlike p, that is an
is an Array: a constant pointer). Therefore, although the previous expression was valid, the following allocation is not:
Look at the following example
Because numbers is an array (constant pointer), and no values can be assigned to constant identifiers. N.B: An array name is just a pointer, nothing more. The array name always points to the first element stored in the array. There for , we can have the following valid C++ code:
The expression *(ara+2) is not vague at all if you remember that an array name is just a pointer that always points to the array’s first element. *(ara+2) takes the address stored in ara, adds 2 to the address, and dereferences that location. Consider the following character array: char name[] = “C++ Programming”; What output do the following cout statements produce?
4.6. Pointer Advantage
You can use pointer notation and reference pointers as arrays with array notation. Study the following program carefully. It shows the inner workings of arrays and pointer notation.
Suppose that you want to store a persons name and print it. Rather than
does just that.
Suppose that you must change a string pointed to by a character pointer, if the person’s name in the above code is changed to Meseter Alemu:
they point to. When we saw the different data types that exist, we saw that some occupy more or less space than others in the memory. For example, in the case of
occupies 4. Let's suppose that we have 3 pointers:
And that we know that they point to memory locations 1000 , 2000 and 3000 respectively. So if we write:
mychar , as you may expect, would contain the value 1001. Nevertheless, myshort would contain the value 2002 , and mylong would contain
and
(^) This is applicable both when adding and subtracting any number to a pointer. It is important to warn you that both increase ( ++ ) and decrease ( -
The first one is equivalent to *(p++) and what it does is to increase p (the address where it points to - not the value that contains).
contain the same number of characters. You can define the table with the following statement.
The above statement will create the following table in memory: Notice that much of the table is waster space. Each row takes 20 characters, even though the data in each row takes far fewer characters. To fix the memory-wasting problem of fully justified tables, you should declare a single-dimensional array of character pointers. Each pointer points to a string in memory and the strings do not have to be the same length. Here is the definition for such an array:
This array is a single-dimension array. The asterisk before names makes this
array an array of pointers. Each string takes only as much memory as is needed by the string and its terminating zero. At this time, we will have this structure in memory: To print the first string, we should use:
To print the second use:
Whenever you dereference any pointer element with the * dereferencing operator, you access one of the strings in the array. 4.9. Pointer to pointer: As the memory address where integer, float or character is stored in can be stored into a pointer variable, the address of a pointer can also be stored in another pointer. This pointer is said to be pointer to a pointer. An array of pointer is conceptually same as pointer to pointer type. The pointer to pointer type is declared as follows:
Note that the asterisk is double here.