我很难将数组的字符值附加到字符串上(handSorted)
。的hand[]
是预定义的文本阵列。
char *handSorted = malloc(strlen(hand)+1);
strcat(handSorted, hand[2]);
例如,我想handSorted
作为字符串hand[2]
'A'的值。
在使用C时,最好学习如何在终端中使用手册页。这是strcat的条目。
DESCRIPTION
The strcat() and strncat() functions append a copy of the
null-terminated string s2 to the end of the null-terminated
string s1, then add a terminating `\0'.
那是一个问题。您需要handSorted为null终止。
char *handSorted = malloc(strlen(hand)+1);
handSorted[0] = '\0';
strcat(handSorted, hand[2]);
但是仍然存在一个问题。hand[2]
是单个字符,而strcat()需要一个字符指针,也就是一个字符串。因此,您需要使用'address-of'运算符-&向其传递字符的地址。像这样。
char *handSorted = malloc(strlen(hand)+1);
handSorted[0] = '\0';
strcat(handSorted, &hand[2]);
我认为这就是我们所追求的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句