简体   繁体   English

C语言中的Printf和Strcpy Bug

[英]Printf and Strcpy Bug in C language

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编码时通常会遇到的两个错误

  1. About printf statements , sometimes upon removing some printf statements i get logical errors but on putting them back the code runs perfectly. 关于printf语句,有时在删除一些printf语句时会出现逻辑错误,但将它们放回原位后,代码可以完美运行。 I dont understand why and do let me know how to avoid those errors too. 我不明白为什么,也让我知道如何避免这些错误。

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) 在下面的代码中,在代码中提到的行(注释指定在该行的前面)在注释掉或删除它之后,“ Answer”变量将接收空白字符串(情况1),而将其取消注释将给出正确的输出(情况2)

  1. About strcpy function, what is the bug or analogy in it that it behaves wierd sometimes 关于strcpy函数,有时它表现得很奇怪是什么错误或类比?

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”函数返回正确的输出,为什么它仍未正确复制到“ Answer”变量中。

在此处输入图片说明

Code is here 代码在这里

#include<stdio.h>
#include<string.h>
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;
}

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将超出范围,使您留下一个杂散的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM