Answer the question
In order to leave comments, you need to log in
How to allocate as much dynamic memory for a variable as needed?
char t[80];
cout << sizeof t << endl; // 80 байт, всё правильно
char *p = new char[80];
cout << sizeof *p << endl; // 1 байт. Почему?
Answer the question
In order to leave comments, you need to log in
In the first case, you get the length of a static array, sizeof works like sizeof(char)*StaticLength.
In the second case, you get the size of the first element pointed to by p. *p --> p[0] where sizeof(p[0] --> char) while *(p+1) --> p[1]
%)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question