开发者

copying c char pointer to a non pointer char

开发者 https://www.devze.com 2023-03-05 01:44 出处:网络
Is there any problem on doing that? char* field = new char[2];开发者_如何学运维 field[0] = \'S\';

Is there any problem on doing that?

char* field = new char[2];开发者_如何学运维
field[0] = 'S';
field[1] = '\0';

char c = *field;

will c always be equal to 'S'?


No problem with that, c will always be 'S'.


This is totally fine - and c will always be equal to 'S'. Dereferencing field, a pointer of type char will result in a char.


There is a problem in that new char[2] is not C syntax. The general idea is correct, though: c will always be 'S'.


There's nothing wrong with that. When you dereference the field array in the line

char c = *field;

what you're actually asking is for the value of the first element of the array, in this case 'S'.

0

精彩评论

暂无评论...
验证码 换一张
取 消