, you must use two asterisks, as in the following example:Ĭhar c = 'A', *cPtr = &c, **cPtrPtr = &cPtr This usage requires careful programming! Without the assignment ptr = &x in Example 9-1, all of the statements containing *ptr would be senselessdereferencing an undefined pointer valueand might well cause the program to crash.Ī pointer variable is itself an object in memory, which means that a pointer can point to it. Of course, the indirection operator * must be used with only a pointer that contains a valid address. Hence the expression *ptr would have the type double. An example:Īs declared here, ptr has the type double * (read: "pointer to double").
Indirection nl how to#
The syntax of the declaration can be seen as an illustration of how to use the pointer. Y = *ptr + 0.5 // Assign y the result of the addition x + 0.5.ĭo not confuse the asterisk ( *) in a pointer declaration with the indirection operator. *ptr = 7.8 // Assign the value 7.8 to the variable x. Dereferencing a pointerĭouble x, y, *ptr // Two double variables and a pointer to double. Hence the expression *ptr is equivalent to the variable x itself. In Example 9-1, ptr points to the variable x.
![indirection nl indirection nl](https://cdn.binqmedia.nl/Woonwijs/2017/8/2/1049246601/14.jpg)
Unlike the multiplication operator *, the indirection operator * is a unary operator that is, it has only one operand. For example, when you access a given location using an int pointer, you read or write an object of type int. The type of the pointer determines the type of object that is assumed to be at that location in memory. Using the indirection operator is sometimes called dereferencing a pointer. If ptr is a pointer, then *ptr designates the object (or function) that ptr points to.
![indirection nl indirection nl](https://cdn.binqmedia.nl/Streetview/bedrijven/2017/9/18/1505656.jpg)
The indirection operator * yields the location in memory whose address is stored in a pointer. Using Pointers to Read and Modify Objects
![indirection nl indirection nl](https://i0.wp.com/www.simtech.nl/wp-content/uploads/2019/09/0OdDULSDZnAiW3hqL.jpeg)
For a complete description of the individual operators in C, with their precedence and permissible operands, see Chapter 5. You can also compare pointers, and use them to iterate through a memory block. The most important of these operations is accessing the object or function that the pointer refers to. This section describes the operations that can be performed using pointers.