Bifalgorithm post · Computer science one · Passing By Reference
How can you check for a NULL pointer and dereference a valid pointer?
How can you check for a NULL pointer and dereference a valid pointer? (Computer science one · Passing By Reference) — "Pointers NULL using the usual equality o…
Ideas in this flowchart
- Pointer validation and dereferencing in C
- Checking for NULL uses the usual equality operator.
- The pointer ptrA is initialized to NULL.
- The condition ptrA == NULL tests for an invalid memory location.
- If ptrA is NULL, the program prints an invalid-memory-location error.
- The dereferencing operator accesses the contents of a referenced memory location.
- Dereferencing requires a valid pointer to a memory location.
- An asterisk placed before a pointer turns it into a regular variable expression.
- Example of assigning an address and changing a value through a pointer
- The integer a is initialized to 10 and ptrA is initialized to NULL.
- ptrA = &a makes ptrA point to a's memory location.
- *ptrA = 20 changes the value stored in a to 20.
- Printing a confirms that its value is 20.
- Figure 18.1 depicts how these pointer operations work in memory.
- Pointers can refer to other variables and access or manipulate their values through dereferencing.