gpt4 book ai didi

C sprintf缓冲区sigsegv错误

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

我的程序有问题:我在 sprintf 函数上不断出现段错误,我不明白为什么,缓冲区足够大,我认为可以正确传递指针,但我就是做不到弄清楚为什么它不起作用。

代码如下:

调用:

char dataBuff[100];
//same error with char *dataBuff=malloc(sizeof(char)*100);
//those vars were declared before
int tmpData[5]={TID,i,JobList[i].Num1,JobList[i].Op,JobList[i].Num2};
//here's the function that return the sigsegv error
BuildCMD(CALC,tmpData,0.f,dataBuff);

构建CMD代码:

int BuildCMD(enum CMD cmd,int *values,float Res,char *dataBuff)
{
switch(cmd)
{//........
case CALC:
{
//this line cause the error,it's just a formatted parameters list
//note:same error with just
//sprintf(dataBuff,"abc");
spritf(dataBuff,"0*;%d;%d;%d;%d;%d;%d;%.5f|\n",cmd,values[0],values[1],values[2],values[3],values[4],Res);
break;
}
//........
}
}

我很确定问题是“dataBuff”,因为即使我使用 sprintf 尝试存储正常的常量字符串,它也会给我同样的错误。只是……我不明白我做错了什么。提前致谢。

编辑:问题已解决作为函数头:

int BuildCMD(enum CMD cmd,int *values,float Res,char dataBuff[100])

作为 sprintf 调用:

sprintf(&dataBuff,"0*;%d;%d;%d;%d;%d;%d;%.5f|\n",cmd,values[0],values[1],values[2],values[3],values[4],Res);

最佳答案

对于 BuildCMD() 来说,参数 dataBuff 是一个指针。但是您已将 dataBuff 声明为指针数组!如果你有字符数据缓冲区[100];那么你可以使用BuildCMD(CALC,tmpData,0.f,dataBuff);

关于C sprintf缓冲区sigsegv错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17010170/

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