Home >Backend Development >C++ >What's the Difference Between `ptr `, ` ptr`, ` *ptr`, and `(*ptr) ` in C ?
Pointers: ptr , ptr, and *ptr
These pointer expressions are often confusing, so let's clarify their meanings:
1. *ptr :
Example:
int arr[] = {1, 2, 3}; int *ptr = arr; cout << *ptr++; // Outputs 1 and then points to the next element (2)
2. * ptr:
Example:
int arr[] = {1, 2, 3}; int *ptr = arr; cout << *++ptr; // Moves the pointer to the next element and outputs 2
3. *ptr:
Caution: Increments the value, not the pointer ptr.
Example:
int *ptr = new int(5); // Points to a dynamically allocated integer cout << ++*ptr; // Outputs 6 and updates the dereferenced integer to 6
4. Bonus: (*ptr) :
Caution: Similar to *ptr, it affects the value, not the pointer itself.
Example:
int *ptr = new int(5); cout << (*ptr)++; // Outputs 5 and updates the dereferenced integer to 6
The above is the detailed content of What's the Difference Between `ptr `, ` ptr`, ` *ptr`, and `(*ptr) ` in C ?. For more information, please follow other related articles on the PHP Chinese website!