![]() ![]() The third one is somewhat obvious, since the second expression was true and the assignment operation performed on foo was foo=&myvar. The second one uses the address-of operator (&), which returns the address of myvar, which we assumed it to have a value of 1776. The first expression is quite clear, considering that the assignment operation performed on myvar was myvar=25. Right after these two statements, all of the following expressions would give true as result: Thus, they have sort of opposite meanings: An address obtained with & can be dereferenced with *.Įarlier, we performed the following two assignment operations: * is the dereference operator, and can be read as "value pointed to by".& is the address-of operator, and can be read simply as "address of".The reference and dereference operators are thus complementary: Notice the difference of including or not including the dereference operator (I have added an explanatory comment of how each of these two expressions could be read):īaz = *foo // baz equal to value pointed to by foo (25) ![]() It is important to clearly differentiate that foo refers to the value 1776, while *foo (with an asterisk * preceding the identifier) refers to the value stored at address 1776, which in this case is 25. This could be read as: "baz equal to value pointed to by foo", and the statement would actually assign the value 25 to baz, since foo is 1776, and the value pointed to by 1776 (following the example above) would be 25. Therefore, following with the values of the previous example, the following statement: The operator itself can be read as "value pointed to by". This is done by preceding the pointer name with the dereference operator (*). ![]() Pointers are said to "point to" the variable whose address they store.Īn interesting property of pointers is that they can be used to access the variable they point to directly. Dereference operator (*)Īs just seen, a variable which stores the address of another variable is called a pointer. A bit later, we will see how to declare and use pointers. Pointers are a very powerful feature of the language that has many uses in lower level programming. The variable that stores the address of another variable (like foo in the previous example) is what in C++ is called a pointer. The main difference between the second and third statements is the appearance of the address-of operator (&). This is a standard assignment operation, as already done many times in earlier chapters. The second statement assigns foo the address of myvar, which we have assumed to be 1776.įinally, the third statement, assigns the value contained in myvar to bar. The values contained in each variable after the execution of this are shown in the following diagram:įirst, we have assigned the value 25 to myvar (a variable whose address in memory we assumed to be 1776). In this case, consider the following code fragment: The actual address of a variable in memory cannot be known before runtime, but let's assume, in order to help clarify some concepts, that myvar is placed during runtime in the memory address 1776. This would assign the address of variable myvar to foo by preceding the name of the variable myvar with the address-of operator (&), we are no longer assigning the content of the variable itself to foo, but its address. The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator. However, it may be useful for a program to be able to obtain the address of a variable during runtime in order to access data cells that are at a certain position relative to it. Fortunately, that task is left to the environment where the program is run - generally, an operating system that decides the particular memory locations on runtime. Generally, C++ programs do not actively decide the exact memory addresses where its variables are stored. When a variable is declared, the memory needed to store its value is assigned a specific location in memory (its memory address). For example, the memory cell with the address 1776 always follows immediately after the cell with address 1775 and precedes the one with 1777. This way, each cell can be easily located in the memory by means of its unique address. These single-byte memory cells are ordered in a way that allows data representations larger than one byte to occupy memory cells that have consecutive addresses. This way, the program does not need to care about the physical address of the data in memory it simply uses the identifier or a symbolic name whenever it needs to refer to the variable.įor a C++ program, the memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address. Earlier, variables have been explained as locations in the computer's memory which can be accessed by their identifier (their name). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |