Pointer Expressions: Navigating ptr , ptr, and *ptr
These pointer expressions are often encountered in C programming and can be tricky to understand. Here's a detailed explanation:
1. *ptr
Example:
int a = 5; int *ptr = &a; *ptr++; // evaluates to 5, then increments ptr to point to the next integer
2. * ptr
Example:
int a = 5; int *ptr = &a; *++ptr; // evaluates to 6 (a++) and increments ptr
3. *ptr
Example:
int a = 5; int *ptr = &a; ++*ptr; // increments a to 6
4. (*ptr)
Example:
int a = 5; int *ptr = &a; (*ptr)++; // evaluates to 5, then increments *ptr to 6
Note that ptr , ptr, and ptr can crash if ptr is an array identifier, while (ptr) can crash if ptr points to a string literal.
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!