![]() That value makes sense to the computer, but it is of no use to programmers. The printf function call above might print this as its value: 3216074448 Now if we were to print the value of ptri, we would get a large number that really makes no sense to us, but makes sense to the computer runtime system. The variable ptri is assigned the address of the variable distance as its value. We can derive the address of a variable by placing a "&" symbol in front of the variable name. All variables have an address, a designation of where they are stored in memory. If pointers contain addresses, there should be a way to give them an address as a value. If we were to use the notation int* ptr1, mem only the first item in the list would be a pointer. This is possible only if we associate the * with the variable name. Note that not everything in the list is a pointer. Here is an example: int *ptr1, width, height, *mem If we associate the * symbol with the variable name, we can declare a list of variable names, some of which are not pointers. Instead, the * symbol is associated with the variable name in the declaration. Since a pointer variable points to another variable of the declared data type, you might expect the declaration to look like this: int* ptr1 In the above examples, the variables are meant to contain the address of other variables, but they have not been initialized yet.Īt first glance, the notation used to declare pointers might seem wrong. That fact might seem intuitive for other data types, but it's hard to remember for pointers. Like all variables, pointer variables do not have a value simply by being declared. These declare ptr1 to hold the address of an integer, ptr2 to hold the address of a floating point number, and ptr3 to hold the address of a character. This is the syntax of a declaration: datatype * variable_name Pointers are declared to point to a typed value. ![]() Pointers "point to" a variable (memory) with a typed value by referencing that variable, not by name, but by address. A pointer is a variable whose value is an address, typed by its declaration. That value is typed, defined by a data type definition in the variable declaration.Ī pointer is no different. We know variables in C are abstractions of memory, holding a value. ![]() And we will see that arrays and pointer are very closely connected. We will discuss how memory can be dynamically allocated and manipulated using pointers. In this chapter, we will discuss pointers and how pointers are used to work with memory. Pointers are a way to get closer to memory and to manipulate the contents of memory directly. Most of these abstractions intentionally obscure something central to storage: the address in memory where something is stored. We have discussed many abstractions that are built into the C programming language. Chapter 8: Pointers and Memory Allocation ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |