为什么我会得到垃圾值以及结果。 [英] Why am I getting garbage values along with the result.

查看:77
本文介绍了为什么我会得到垃圾值以及结果。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

合并两个字符串。我找到了两个字符串的长度并尝试使用下面的循环合并它们。



Char s1 [20],s2 [20];

Int i = 0;

Int k,len1,len2; // len1和len2是2个字符串的长度



For(k = len1; k< len1 + len2; k ++)

{

s1 [k] = s2 [i];

++ i;

}



我得到了正确的输出,但最后还附加了4个垃圾值。原因请帮助。



我尝试了什么:



我发现另一个循环相同。

For(i = 0; i< len2; i ++)

{

s1 [len1 + i] = s2 [i];

}



但我想知道程序中的错误。

谢谢

Merge two strings. I found the length of two strings and trying to merge them using the below loop.

Char s1 [20],s2 [20];
Int i=0;
Int k, len1,len2; //len1 and len2 are lengths of 2 strings

For (k=len1;k <len1+len2;k++)
{
s1 [k] = s2 [i];
++i;
}

I got correct output but 4 garbage values are also appended in the end. Reasons pls.

What I have tried:

I found another loop for the same.
For (i=0;i <len2;i++)
{
s1 [len1+i] = s2 [i];
}

But i wanted to know the error in my program.
Thanks

推荐答案

正如OriginalGriff所说,你需要正确终止你的字符串:

As OriginalGriff says, you need to terminate your string properly thus:
For (k=len1;k <len1+len2;k++) 
{
s1 [k] = s2 [i];
++i;
} 
s1[k] = '\0'; // add the terminating null to the end of the array



您还应该检查len1 + len2是否小于20.


You should also check that len1 + len2 is less than 20.


有几个原因。
第一个是你正在尝试将两个长度相等的字符串 - 每个字符串20个 - 附加到为其中一个分配的空间中。如果len1和len2值加起来小于20,则表示没有问题。但是如果它们超过了那个,那么你就是在一个阵列或另一个阵列的运行结束,这是一个问题因为你不知道你正在使用什么内存,所以你不知道你在覆盖什么。编译器不会告诉你,并且没有标准可以定义它,因为首先做它是一个糟糕的坏主意!定义一个足以容纳两个字符串的输出字符串区域,以及一个额外的字符。



这个额外的字符引出了第二个原因:C中的字符串只是一个字符数组,以包含空值的字符结尾 - '\0'

由于您没有复制或添加'\ 0'到输出的末尾,当您打印字符串时,它不会停止打印字符,直到它到达只有内存位置碰巧包含null。因此,您会在预期数据后打印随机数字的随机字符。
Quite a few reasons.
The first one is that you are trying to append two strings of equal length - 20 characters each - into the space allocated for one of them. If both len1 and len2 values add up to less than 20, you don't have a problem. But if they exceed that, then you are "running off the end" of one array or the other, and that's a problem because you don't know what memory you are using, so you don't know what you are overwriting. The compiler isn't going to tell you, and there is no standard that will define that because it's a bad, bad idea to do it in the first place! Define an output string area that is big enough to hold both strings, plus an extra character.

That extra character leads us to the second reason: strings in C are just an array of characters, terminated with a character containing a null value - '\0'
Since you don't copy or add a '\0' to the end of your output, when you print the string, it doesn't stop printing characters until it reaches a memory location that just happens to contain null. Hence you get a random number of random characters printed after your expected data.


尝试

Try
#include <stdio.h>

// merges strings 's1', 's2' into bufer 'buf', having size 'bufsize'
// returns 0 on success.
int merge( const char * s1, const char * s2, char * buf, size_t bufsize)
{
  const char * sa[2] = {s1,  s2 };

  size_t i = 0;
  size_t k = 0;
  for ( i = 0; i<2; ++i)
  {
    const char *p = sa[i];
    while ( *p )
    {
      if ( k == bufsize) return -1;
      buf[k] = *p;
      ++k;
      ++p;
    }
  }
  if ( k == bufsize) return -1;
  buf[k] = '\0';
  return 0;
}

// usage example
int main()
{
  const char * s1 = "foo";
  const char * s2 = "bar";
  char m[20];
  int rc = merge(s1, s2, m, sizeof(m));
  if (  rc )
    printf("unable to merge\n");
  else
    printf("merged string '%s'\n", m);

  return 0;
}


这篇关于为什么我会得到垃圾值以及结果。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆