简体   繁体   English

strlen 在函数中改变输出

[英]strlen in a function changing output

Okay, I tried to figure it out, but couldn't;好吧,我试图弄清楚,但不能;

So there is a function in my source code with two arguments passed (9 and 1, as Strings)所以我的源代码中有一个函数,传递了两个参数(9 和 1,作为字符串)

char * plus( char *a, char *b ){
   char sa[101] = {0};
   char sb[101] = {0};
   static char _s[101] = {0};
   int i;
   int st = 0;
   int sto;
   for( i = strlen( a ) - 1; i >= 0; i-- ){
       sa[strlen( a ) - 1 - i] = a[i]; 
   }
   for( i = strlen( b ) - 1; i >= 0; i-- ){
       sb[strlen( b ) - 1 - i] = b[i];
   }
   for( i = 0; i < strlen( sa ); i++ ){
       sto = 0;
       if(( sa[i] + sb[i] + st - 2 * '0' ) > 9 ) sto = 1;
       sa[i] = ( sa[i] + sb[i] + st - 2 * '0' ) %10 + '0';
       st = sto;
       if( st && sa[i + 1] == 0) sa[i + 1] = '1';
   }
   for( i = strlen( sa ) - 1; i >= 0; i-- ){
       _s[strlen( sa ) - 1 - i] = sa[i];
   }
   return _s;
}

It should return "10" but instead "*0", but constantly, (so not "undefined behaviour") What's wrong?它应该返回“10”而不是“*0”,但不断地,(所以不是“未定义的行为”)有什么问题?

Edit:编辑:

if I pass strlen( a ) instead of strlen( sa ) in line for( i = 0; i < strlen( sa ); i++ ){ then it works correctly如果我在for( i = 0; i < strlen( sa ); i++ ){传递strlen( a )而不是strlen( sa ) ,那么它可以正常工作

sa[i] + sb[i] + st - 2 * '0' sa[i] + sb[i] + st - 2 * '0'

is negative.是否定的。 Because you do sa[i + 1] = '1' then strlen(sa) get's incremented in the loop.因为你做sa[i + 1] = '1'然后strlen(sa) get's 在循环中递增。 Because of that, sb[i] is equal 0 because it's the zero terminating character on the last run when i == strlen(sa) == 2 .因此, sb[i]等于 0,因为当i == strlen(sa) == 2时,它是最后一次运行的零终止字符。 Then the calculation becomes:那么计算就变成了:

sa[i] + sb[i] + st - 2 * '0' = 
  '1' +     0 +  1 - 2 * '0' = 
   49 +     0 +  1 - 2 * 48  =
                50 - 96      =
                -46 

Because that's minus 46, sa[i] becomaes:因为那是负 46,所以 sa[i] 变成:

a[i] = ( sa[i] + sb[i] + st - 2 * '0' ) %10 + '0' =
                                    -46 %10 + '0' =
                                         -6 + '0' =
                                              '*'

Most probably in the loop:最有可能在循环中:

 for( i = 0; i < strlen( sa ); i++ ){

You have to take care what happens when string lengths are not equal.您必须注意当字符串长度不相等时会发生什么。 You do sa[i] = '1' , but sb stays the same.你做sa[i] = '1' ,但sb保持不变。 I guess for now you can fix it by replacing我想现在你可以通过更换来修复它

  sa[i] + sb[i] + st - 2 * '0'

with something along:伴随着一些东西:

  sa[i] - '0' + (sb[i] ? sb[i] - '0' : '0') + st

Notes: I do not enjoy your indentation.笔记:我不喜欢你的缩进。 Please make your code as readable as possible.请使您的代码尽可能具有可读性。 Use newlines to denote new expressions.使用换行符来表示新的表达式。 Use { } to denote where you enter loop or if bodies.使用{ }表示您进入循环或 if 正文的位置。

This loop这个循环

   for( i = 0; i < strlen( sa ); i++ ){
       sto = 0;
       if(( sa[i] + sb[i] + st - 2 * '0' ) > 9 ) sto = 1;
       sa[i] = ( sa[i] + sb[i] + st - 2 * '0' ) %10 + '0';
       st = sto;
       if( st && sa[i + 1] == 0) sa[i + 1] = '1';
   }

is incorrect because within the loop the array sa is being changed.不正确,因为在循环内数组sa正在更改。 So also the value returned by strlen( sa ) that is calculated in each iteration is also changed.因此,在每次迭代中计算的strlen( sa )返回的值也发生了变化。

When you are using strlen( a ) instead of strlen( sa ) then the array s is not changed in the loop .当您使用strlen( a )而不是strlen( sa )时,循环中的数组 s 不会更改。 So the value returned by the expression is fixed.所以表达式返回的值是固定的。

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

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