Here i wrote a piece of code. A function to add long numbers (used strings to represent numbers).

这里我写了一段代码。添加长数字的函数(使用字符串表示数字)。

I want to know about two bugs that I usually face while coding in C

我想知道在C编码时我通常会面对的两个错误。

About printf statements , sometimes upon removing some printf statements i get logical errors but on putting them back the code runs perfectly. I dont understand why and do let me know how to avoid those errors too.

关于printf语句,有时在删除一些printf语句时,我得到了逻辑错误,但是在将它们放回代码时,代码运行得很好。我不明白为什么,也一定要让我知道如何避免这些错误。

Eg. In below code, the line mentioned in code (comments specified too infront of that line) after commenting it back or removing it, "Answer" variable receives blank string(case 1) and uncommenting it back gives correct output(case 2)

如。在下面的代码中,代码中所提到的行(在该行前面指定的注释)在注释掉它或删除它之后,“回答”变量会接收到空白字符串(情况1),并取消注释,给出正确的输出(情况2)

About strcpy function, what is the bug or analogy in it that it behaves wierd sometimes

关于strcpy函数,它的bug或类比是什么,它有时表现得很糟糕。

Eg. In above mentioned case 2, even though the "add" function is returning correct output why is it not getting copied correctly into the "Answer" variable.

如。在上面提到的情形2中,即使“add”函数返回正确的输出,为什么它不能正确地复制到“应答”变量中。

Code is here

代码在这里

#include

#include

char *strrev(char *str)

{

char *p1, *p2;

if (! str || ! *str)

return str;

for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)

{

*p1 ^= *p2;

*p2 ^= *p1;

*p1 ^= *p2;

}

return str;

}

char* add(char a[],char b[]){

int n1=strlen(a);

int n2=strlen(b);

char c[20],d[20];

int i,j,k=0,carry=0,sum=0;

for(i=n1-1,j=n2-1;i>=0 && j>=0;i--,j--){

sum = (a[i]-'0')+(b[j]-'0')+carry;

c[k++]=sum%10 + '0';

carry=sum/10;

}

if(i>=0){

for(;i>=0;i--){

sum = (a[i]-'0')+carry;

c[k++]=sum%10 + '0';

carry=sum/10;

}

}

else if(j>=0){

for(;j>=0;j--){

sum = (b[j]-'0')+carry;

c[k++]=sum%10 +'0';

carry=sum/10;

}

}

if(carry){

while(carry){

c[k++]=carry%10 + '0';

carry/=10;

}

}

c[k]='\0';

printf("\nResult under function = %s",strrev(c)); //upon removing this printf statement the Result variable in main() receives a blank string

return strrev(c);

}

int main(){

char answer[20];

printf("\nAnswer = %s\n",add("0","1"));

strcpy(answer,add("0","1"));

printf("\nNow Answer is %s \n",answer); // Here is the bug

return 0;

}

1 个解决方案

#1

3

You have undefined behavior because you return a pointer to a local variable. The array c in the add function will go out of scope once the function returns, leaving you with a stray pointer.

您有未定义的行为,因为您返回一个指向局部变量的指针。当函数返回时,add函数中的数组c将超出范围,留给您一个离散指针。

c语言strcpy错误,C语言中的Printf和Strcpy错误。相关推荐

  1. c语言中1B是多少,C语言1、C语言中,运算对象必须是整型的运算符是【】A./B-查字典问答网...

    C语言1.C语言中,运算对象必须是整型的运算符是[]A./B.%C.++D. 2.下列标识符中,不合法的是[] A.x2B.sizeofC._123D.char2 5.设d是double型变量,若要从 ...

  2. c语言putchar函数_C语言中的putchar()函数与示例

    c语言putchar函数 C语言中的putchar()函数 (putchar() function in C) The putchar() function is defined in the < ...

  3. c语言中的printf函数_C语言中的printf()函数与示例

    c语言中的printf函数 C语言中的printf()函数 (printf() function in C) The printf() function is defined in the <s ...

  4. c语言编译undeclared,c语言中undeclared identifier是什么意思?,undeclared identifier c语言错误 怎么改...

    c语言中undeclared identifier是什么意思?c语言中未声明的标识符是什么意思?,"未声明的标识符"指"未声明的标识符",是c语言中的错误提示: ...

  5. c语言 strcpy作用,c语言中strcpy的用法

    c语言中strcpy的用法的用法你知道吗?下面小编就跟你们详细介绍下c语言中strcpy的用法的用法,希望对你们有用. c语言中strcpy的用法的用法如下: strcpy原型声明:extern ch ...

  6. r语言中c函数错误,R语言中c()函数与paste()函数的区别说明

    c()函数:将括号中的元素连接起来,并不创建向量 paste()函数:连接括号中的元素 例如 c(1, 2:4),结果为1 2 3 4 paste(1, 2:4),结果为"1 2" ...

  7. C语言程序设计double,C语言中double类型数据占字节数为

    1以下选项中,能用作数据常量的是________. A.o115 B.0118 C.115L D.1.5e1.5 2下列叙述中错误的是_______. A.一个C语言程序只能实现一种算法 B.一个C函 ...

  8. c语言教程 define,C语言中define的用法

    预处理命令以"#"号开头,如包含命令#include,宏定义命令#define等.一般都放在源文件的前面,它们称为预处理部分. 所谓预处理是指在进行编译之前所作的工作.预处理是C语 ...

  9. c语言void结尾,C语言中void*详解及应用

    void在英文中作为名词的解释为"空虚:空间:空隙":而在C语言中,void被翻译为"无类型",相应的void *为"无类型指针".void ...

最新文章

  1. opengl库区分:glut、freeglut、glfw、glew、gl3w、glad
  2. IPNC RDK5.0 make sysall 无法通过问题
  3. R语言实战-统计分析基础-描述性统计4-psych-describe
  4. 数据挖掘 python roc曲线_利用scikitlearn画ROC曲线实例
  5. arcgis已试图对空几何执行该操作_ArcGIS中地理配准与空间校正的不同
  6. 全连接条件随机场_条件随机场CRF简介
  7. 新微擎 v1.7.9 图文回复 标题emoji
  8. @propertysource 读不到properties_敢不敢来挑战,读对一班就是学霸
  9. Oracle函数-分析函数-获取上一条(下一条)记录
  10. [渝粤教育] 西南科技大学 液压与气压传动 在线考试复习资料
  11. 游戏多开的原理与方法
  12. 【PS】如何使用Photoshop调整图片指定部分的大小
  13. 分享各类优秀与设计师制作公司相关的资源网站
  14. Uber的启动画面是如何制作的
  15. 【汇正财经】什么是板块轮动?有什么规律?
  16. 1.1.7. Least Angle Regression(最小角回归)和 1.1.8. LARS Lasso
  17. [JavaScript学习-01]JavaScript实现九宫格抽奖
  18. Excel制作随机点名小程序
  19. [C++贪心习题]压缩歌曲
  20. 数智赋能跨境国际物流暨WallTech沃行信息十周年庆典圆满落幕

热门文章

  1. 完全背包问题 POJ1384
  2. Servlet基础(一) Servlet基础和关键的API介绍
  3. 热烈祝贺Polymer中文组织站点上线
  4. easyui使用datagrid时列名包含特殊字符导致表头与数据错位的问题
  5. 百度地图2.1获取密钥配置的Android签名证书的sha1值
  6. C/C++语言之 日期 时间
  7. 无人驾驶岗平均月薪4.14万:因为稀缺,所以高薪
  8. setsockopt()函数使用详解
  9. 关于将一个数输出为原码、反码和补码的解惑
  10. hashmap的五种便利方式