我如何在 C 中 return 一个字符串?
How do I return a string in C?
所以我试图做一个关于哈佛 CS50 课程的问题集。我能够创建该程序,但希望在设计方面使其更简洁。我决定使用一个函数。
目标是创建一个程序,该程序采用 1 到 8 之间的高度值。如果高度为 1,则输出将为 # #
。如果高度增加,那么输出将包括另外 2 个玩家,左右各有 1 个额外的 #
。
当我尝试 return 一个值并创建一个 return 类型为 string
的函数时,我不断出错。
代码:
#include <stdio.h>
#include <cs50.h>
char *mario();
int main(void){
printf("%c", mario());
}
char *mario(){
int stop = 0;
while(stop == 0){
unsigned int height = get_int("Height: ");
char *result = " ";
if(height == 1){
printf("\n# #\n");
stop = 1;
}else if(height == 2){
result = "\n # #\n## ##\n";
stop = 1;
}else if(height == 3){
result = "\n # #\n ## ##\n### ###\n";
stop = 1;
}else if(height == 4){
result = "\n # #\n ## ##\n ### ###\n#### ####\n";
stop = 1;
}else if(height == 5){
result = "\n # #\n ## ##\n ### ###\n #### ####\n##### #####\n";
stop = 1;
}else if(height == 6){
result = "\n # #\n ## ##\n ### ###\n #### ####\n ##### #####\n###### ######\n";
stop = 1;
}else if(height == 7){
result = "\n # #\n ## ##\n ### ###\n #### ####\n ##### #####\n ###### ######\n####### #######\n";
stop = 1;
}else if(height == 8){
result = "\n # #\n ## ##\n ### ###\n #### ####\n ##### #####\n ###### ######\n ####### #######\n######## ########\n";
stop = 1;
}
}
return result;
}
错误:
mario.c:33:7: error: conflicting types for 'mario'
char *mario(){
^
mario.c:5:5: note: previous declaration is here
int mario(void);
^
mario.c:88:12: error: use of undeclared identifier 'result'
return result;
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
我该如何解决这个问题?
我正在使用所见的 CS50 库和 IDE。
将int mario(void);
改为char *mario(void);
将printf("%c", mario());
改为printf("%s", mario());
将 char *result = " ";
移动到 mario()
函数的开头,使其在 return
.
的范围内
所以我试图做一个关于哈佛 CS50 课程的问题集。我能够创建该程序,但希望在设计方面使其更简洁。我决定使用一个函数。
目标是创建一个程序,该程序采用 1 到 8 之间的高度值。如果高度为 1,则输出将为 # #
。如果高度增加,那么输出将包括另外 2 个玩家,左右各有 1 个额外的 #
。
当我尝试 return 一个值并创建一个 return 类型为 string
的函数时,我不断出错。
代码:
#include <stdio.h>
#include <cs50.h>
char *mario();
int main(void){
printf("%c", mario());
}
char *mario(){
int stop = 0;
while(stop == 0){
unsigned int height = get_int("Height: ");
char *result = " ";
if(height == 1){
printf("\n# #\n");
stop = 1;
}else if(height == 2){
result = "\n # #\n## ##\n";
stop = 1;
}else if(height == 3){
result = "\n # #\n ## ##\n### ###\n";
stop = 1;
}else if(height == 4){
result = "\n # #\n ## ##\n ### ###\n#### ####\n";
stop = 1;
}else if(height == 5){
result = "\n # #\n ## ##\n ### ###\n #### ####\n##### #####\n";
stop = 1;
}else if(height == 6){
result = "\n # #\n ## ##\n ### ###\n #### ####\n ##### #####\n###### ######\n";
stop = 1;
}else if(height == 7){
result = "\n # #\n ## ##\n ### ###\n #### ####\n ##### #####\n ###### ######\n####### #######\n";
stop = 1;
}else if(height == 8){
result = "\n # #\n ## ##\n ### ###\n #### ####\n ##### #####\n ###### ######\n ####### #######\n######## ########\n";
stop = 1;
}
}
return result;
}
错误:
mario.c:33:7: error: conflicting types for 'mario'
char *mario(){
^
mario.c:5:5: note: previous declaration is here
int mario(void);
^
mario.c:88:12: error: use of undeclared identifier 'result'
return result;
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
我该如何解决这个问题?
我正在使用所见的 CS50 库和 IDE。
将
int mario(void);
改为char *mario(void);
将
printf("%c", mario());
改为printf("%s", mario());
将
char *result = " ";
移动到mario()
函数的开头,使其在return
. 的范围内