我正在写一个小C程序,想知道为什么我在控制台中的输出是" 0"," 0" [...]我期望的输出是" ab"," ac",[...]。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
int j;
char string[] = "abc";
char output[8];
int length = size(&string[0]);
for(i=0; i<length; i++) {
for(j=0; j<length; j++){
char a = string[i];
strcat(output, &a);
char b = string[j];
strcat(output, &b);
printf("%c\n", output);
}
}
return 0;
}
答案 0 :(得分:4)
错误#1。您尚未初始化output[]
,因此strcat()
无法有效地找到要附加的nul
终止符。
output[0] = 0;
错误#2。 strcat()
无论如何都不是追加字符的正确方式。
错误#3。你的循环控制是不对的。见下文。
错误#4。您的length
是char*
指针的大小。
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i, j;
char string[] = "abc";
char output[8];
int length = strlen (string); // corrected
for(i=0; i<length-1; i++) { // amended loop
for(j=i+1; j<length; j++) { // amended loop
output[0] = string [i];
output[1] = string [j];
output[2] = 0; // string terminator
printf("%s\n", output); // uses string type not char
}
}
return 0;
}
节目输出:
ab
ac
bc
答案 1 :(得分:2)
如果我已经正确理解了您要做的事情,那么该程序将采用以下方式
#include <stdio.h>
int main(int argc, char *argv[])
{
char string[] = "abc";
char output[3];
size_t length = sizeof( string ) - 1;
for ( size_t i = 0; i < length; i++ )
{
for ( size_t j = 0; j < length; j++ )
{
if ( i != j )
{
output[0] = string[i];
output[1] = string[j];
output[2] = '\0';
puts( output );
}
}
}
return 0;
}
输出
ab
ac
ba
bc
ca
cb
如果你的编译器不允许在循环的控制语句中声明变量,那么你可以在程序的开头声明i和j。
size_t i, j;
如果你想包含像“aa”这样的组合,你可以简单地删除内部循环的if语句。
答案 2 :(得分:0)
char a = string[i];
strcat(output, &a);
导致未定义的行为,因为strcat
期望第二个参数中的空终止字符串。同样的事情适用于:
char b = string[j];
strcat(output, &b);
也许您打算使用:
output[0] = a;
output[1] = b;
output[2] = '\0';
这是更新的for
循环:
for(i=0; i<length; i++) {
for(j=0; j<length; j++){
output[0] = a;
output[1] = b;
output[2] = '\0';
printf("%s\n", output);
// ^^ use %s to print a string, not %c.
}
}
答案 3 :(得分:0)
如果你想使用strcat
,你必须知道它希望字符串不是字符而且有一个重要区别,当你通过&a
strcat
认为它是地址时一个指向字符串的指针,你最有可能是segmentation fault
,在这里我展示你自己的代码,修改为使用strcat
,但你并不需要它来完成这项任务。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
int j;
char string[] = "abc";
int length = strlen(&string[0]);
for(i = 0 ; i < length ; i++)
{
for(j= i + 1 ; j < length ; j++)
{
/* initialize output to 0 more importantly to have a terminating null byte */
char output[3] = {0};
/*
* create a string and initialize it with 2 char's
* - the first one, the one you want to append to output
* - the second one is required by strcat, to mark the end of the string
*/
char a[2] = {string[i], 0};
strcat(output, a);
/* same as above */
char b[2] = {string[j], 0};
strcat(output, b);
printf("%s\n", output);
}
}
return 0;
}
你可以在没有strcat
的情况下执行此操作,除非您尝试学习如何使用strcat
,这是一个如何在没有strcat
的情况下执行此操作的示例。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i;
int j;
char string[] = "abc";
int length = strlen(&string[0]);
for(i = 0 ; i < length ; i++)
{
for(j= i + 1 ; j < length ; j++)
{
char output[3] = {string[i], string[j], 0};
printf("%s\n", output);
}
}
return 0;
}