Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将数组中的字符转换为字符串_C_Arrays_String - Fatal编程技术网

C 如何将数组中的字符转换为字符串

C 如何将数组中的字符转换为字符串,c,arrays,string,C,Arrays,String,我很难将数组的字符值附加到字符串(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 c

我很难将数组的字符值附加到字符串
(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'.  
这是一个问题。您需要手动排序才能以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]);

我想这就是我们要找的。

在处理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'.  
这是一个问题。您需要手动排序才能以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]);

我想这就是我们要找的。

在处理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'.  
这是一个问题。您需要手动排序才能以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]);

我想这就是我们要找的。

在处理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'.  
这是一个问题。您需要手动排序才能以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]);

我想这就是我们要找的。

strcat函数期望两个参数都是以null结尾的字符串(指针),当您按值传递到字符时,这将导致未定义的行为(可能是segfault崩溃,因为它会尝试读取可能位于分配区域之外的低内存地址)

如果要添加单个字符,最好直接设置字符值:

size_t charLength = strlen( handSorted );
assert( charLength < sizeof( handSorted ) + 1 ); // assuming handSorted hasn't decomposed from char[N] to char*.
handSorted[ charLength     ] = hand[2]; // overwrite existing null-terminator with desired char
handSorted[ charLength + 1 ] = '\0'; // set new null-terminator
size\u t charLength=strlen(手动排序);
断言(charLength
strcat函数期望两个参数都是以null结尾的字符串(指针),当您按值传递到字符时,这将导致未定义的行为(可能是segfault崩溃,因为它将尝试读取可能位于分配区域之外的低内存地址)

如果要添加单个字符,最好直接设置字符值:

size_t charLength = strlen( handSorted );
assert( charLength < sizeof( handSorted ) + 1 ); // assuming handSorted hasn't decomposed from char[N] to char*.
handSorted[ charLength     ] = hand[2]; // overwrite existing null-terminator with desired char
handSorted[ charLength + 1 ] = '\0'; // set new null-terminator
size\u t charLength=strlen(手动排序);
断言(charLength
strcat函数期望两个参数都是以null结尾的字符串(指针),当您按值传递到字符时,这将导致未定义的行为(可能是segfault崩溃,因为它将尝试读取可能位于分配区域之外的低内存地址)

如果要添加单个字符,最好直接设置字符值:

size_t charLength = strlen( handSorted );
assert( charLength < sizeof( handSorted ) + 1 ); // assuming handSorted hasn't decomposed from char[N] to char*.
handSorted[ charLength     ] = hand[2]; // overwrite existing null-terminator with desired char
handSorted[ charLength + 1 ] = '\0'; // set new null-terminator
size\u t charLength=strlen(手动排序);
断言(charLength
strcat函数期望两个参数都是以null结尾的字符串(指针),当您按值传递到字符时,这将导致未定义的行为(可能是segfault崩溃,因为它将尝试读取可能位于分配区域之外的低内存地址)

如果要添加单个字符,最好直接设置字符值:

size_t charLength = strlen( handSorted );
assert( charLength < sizeof( handSorted ) + 1 ); // assuming handSorted hasn't decomposed from char[N] to char*.
handSorted[ charLength     ] = hand[2]; // overwrite existing null-terminator with desired char
handSorted[ charLength + 1 ] = '\0'; // set new null-terminator
size\u t charLength=strlen(手动排序);
断言(charLength
你用什么语言编码?在C中,抱歉现在将其添加到标记中。你可以使用for循环,你可以使用memset,你可以使用strdup和许多其他/你用什么语言编码?在C中,抱歉现在将其添加到标记中。你可以使用for循环,你可以使用memset,你可以使用strdup和许多其他/你用什么语言编码?在C中,抱歉,现在将其添加到标记中。你可以使用for循环,你可以使用memset,你可以使用strdup和其他许多/你用什么语言编码?在C中,抱歉现在将其添加到标记中。你可以使用for循环,你可以使用memset,你可以使用strdup和其他许多/非常感谢你,我已经有了空终止,忘了愚蠢地添加,但是角色指针帮了我很多!非常感谢。戴,你能再解释一下吗?我提供的这个代码片段非常简短和机智