#include<iostream>
using namespace std;
int main()
{
char ch='a';
char *p=&ch;
cout<<sizeof(p);
return 0;
}
Mình không hiểu tại sao cái sizeof(p)
lại ra 8 trong khi kiểu char
là 1 xin mọi người giải đáp thắc mắc
#include<iostream>
using namespace std;
int main()
{
char ch='a';
char *p=&ch;
cout<<sizeof(p);
return 0;
}
Mình không hiểu tại sao cái sizeof(p)
lại ra 8 trong khi kiểu char
là 1 xin mọi người giải đáp thắc mắc
p là con trỏ chứ có phải char đâu?
Câu trả lời hay và cùng ý mình muốn trả lời cho câu hỏi này (từ StackOverflow)
sizeof(p)
is the size of the pointer itself. It depends on the size of the address bus. Which means for a 64-bit system, the address bus size will be 64-bit (8 bytes) so pointer will be 8 bytes long (that shows your system is 64-bit). And on a 32-bit system, it’s size will be 32-bit(4 bytes).
sizeof(*p)
is the size of pointer type i.e.int
here. So usuallyint
is of 32-bit long that is 4 bytes.