Home >Backend Development >C++ >Are C Pointer Sizes Fixed or Variable, and How Do Function Pointers Differ?
Pointer Size: Fixed or Variable?
In C , pointers have a fixed size, which differs based on the underlying architecture. Typically, on 32-bit systems, pointers occupy 32 bits of memory, while on 64-bit systems, they take up 64 bits. This fixed size is evident from the following code snippet:
int x = 10; int *xPtr = &x; char y = 'a'; char *yPtr = &y; std::cout << sizeof(x) << "\n"; std::cout << sizeof(xPtr) << "\n"; std::cout << sizeof(y) << "\n"; std::cout << sizeof(yPtr) << "\n";
Output:
4 4 1 4
As you can observe, the sizes of xPtr and yPtr are both 4, regardless of the types they point to. This is because pointers store memory addresses, which have a fixed size determined by the architecture.
However, it's important to note that function pointers behave differently. They generally occupy more space than standard pointers, as they contain additional information about the function prototype.
The above is the detailed content of Are C Pointer Sizes Fixed or Variable, and How Do Function Pointers Differ?. For more information, please follow other related articles on the PHP Chinese website!