gpt4 book ai didi

使用可变参数函数连接字符串

转载 作者:行者123 更新时间:2023-11-30 15:30:09 25 4
gpt4 key购买 nike

我制作了一个程序,它接收 2 到 5 个字符串,并使用可变参数函数将所有字符串连接在一起。

到目前为止,程序运行正常,但在显示完整字符串之前,它总是在末尾显示 3 个随机字符。

例如:

Please insert number of strings: 3
string 1: car
string 2: bike
string 3: plane

完整字符串:

=$>carbikeplane

我对程序进行了几次调整,试图找到原因并修复它,但我总是得到相同的结果。

完整的程序如下所示。关于该计划的一些评论:

  1. 我正在程序的不同部分打印字符串,因为我试图找到问题所在。所以有些 printf() 函数可能没有意义。
  2. main函数看起来没问题,问题出在后面定义的函数上。

注意:我仍在学习 C,所以可能有一些代码可以/可能会创建未定义的行为,如果有的话,如果您能指出这些,我将不胜感激。

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

char *function(int num, ...);

int main(void)
{
char line1[80] = " ", line2[80] = " ", line3[80] = " ", line4[80] = " ", line5[80] = " ";
int count = 0, count2;
char *newStr;
int i;
int status;

do {
fflush(stdin);
printf("\nPlease select the number of strings (max. 5): ");
scanf("%d", &count);

}while(count < 2 && count > 5);

count2 = count;
fflush(stdin);

status = 1;
for( i = 1 ; count > 0; count--, i++)
{
switch(status)
{
case 1:
{
printf("\nInput string[%d]: ", i);
gets(line1);
status = 2;
break;
}
case 2:
{
printf("\nInput string[%d]: ", i);
gets(line2);
status = 3;
break;
}
case 3:
{
printf("\nInput string[%d]: ", i);
gets(line3);
status = 4;
break;
}
case 4:
{
printf("\nInput string[%d]: ", i);
gets(line4);
status = 5;
break;
}
case 5:
{
printf("\nInput string[%d]: ", i);
gets(line5);
status = 6;
break;
}
}

}
printf("\n%s\n%s\n%s\n%s\n%s\n", line1, line2, line3, line4, line5);
/*call the function of variable arguments*/
/*memory allocation of newstr*/
newStr = (char *)malloc(420*sizeof(char) +1);
switch(count2)
{
case 2:
{
newStr = function(2, line1, line2);
break;
}
case 3:
{
newStr = function(3, line1, line2, line3);
break;
}
case 4:
{
newStr = function(4, line1, line2, line3, line4);
break;
}
case 5:
{
newStr = function(5, line1, line2, line3, line4, line5);
}
}

printf("\nThe final string is: \n");
printf("%s", newStr);

return 0;

}


char *function(int num, ...)
{
va_list arg_ptr;
int b;
char *string;
char *curstr;

va_start(arg_ptr, num); /*initialize the arg_ptr*/

string = (char *)malloc(420*sizeof(char) + 1);
*string = " ";

for(b=0; b < num; b++)
{
curstr = va_arg(arg_ptr, char * );
string = strcat(string,curstr);
}

printf("\n%s", string);

va_end(arg_ptr);

return string;

}

最佳答案

真正的问题是您可以编译以下行: *string = ""; 这不再有效,不应编译。假设您将该行放在那里来初始化字符串以具有初始值。但这可以通过像这样分配字符串来轻松解决:

string = calloc(420, sizeof(char));

即:使用calloc将内存设置为零。这样你就有了一个可以被 strcat 使用的有效字符串。

我并不是告诉您不要使用 getsfflush 因为很明显这是一个家庭作业并且建议使用 fgets > 在处理输入字符串时有其自身的问题。当然,如果您在生产代码中使用 gets ,那时就会有人踢您。

关于再次转换 malloc 的返回值,这是一把双面剑。如果您确定要将项目编译为 C 项目(即:文件名以 .c 结尾并且使用 gcc 进行编译),那么可以。不要施放。但是在其他情况下,例如将文件命名为 .cpp 或使用 g++ 进行编译......好吧。您将收到错误:error: invalid conversion from ‘void*’ to ‘char*’ without the Cast。我有一种感觉,在初学者的水平上,当你为学校做家庭作业时,你或多或少地专注于让你的代码编译和运行,而不是拘泥于迂腐。但对于 future ,建议你迂腐一些。

关于使用可变参数函数连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25760453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com