简体   繁体   English

为什么strcmp()返回'0'虽然我没有提供空终止字符串?

[英]Why strcmp() returning '0' though I am not providing null terminated string?

In the below code I was expecting some weird behavior(like infinite loop) as I am not providing null terminated string as an input to strcmp() function. 在下面的代码中,我期待一些奇怪的行为(如无限循环),因为我没有提供空终止字符串作为strcmp()函数的输入。

#include <stdio.h>

int main() 
{
  char string1[20];
  char string2[20] = {'H','e','l','l','o',};

  strcpy(string1, "Hello");
 // strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strcmp( string1, string2));

  return 0;
}

The output is: 输出是:

 Return Value is : 0

Why it is showing me both strings are equal? 为什么它向我显示两个字符串相等?

My guess is when I initialized array (string2)rest elements are filled with zero.But as the array is local this should not be the case. 我的猜测是当我初始化数组(string2)时,rest元素被填充为零。但是因为数组是本地的,所以不应该是这种情况。

If an initialiser to an array or struct only provides data for parts of the array or struct , the rest of the array or struct is set to 0 . 如果数组或结构的初始化程序仅为数组或结构的某些部分提供数据,则数组或结构的其余部分将设置为0 This is the case for string2 . string2的情况就是这样。

I dumped the string2 and I got 我扔了string2 ,我得到了

72 101 108 108 111 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

The rest of the string2 is filled with 0 s string2的其余部分填充0

for (i = 0; i < 20; i++)
    printf("%d ", string2[i]);

And when I did this, I got 当我这样做时,我得到了

char string2[20];

string2[0] = 'H';
string2[1] = 'e';
string2[2] = 'l';
string2[3] = 'l';
string2[4] = 'o';

72 101 108 108 111 127 0 0 -27 5 64 0 0 0 0 0 -1 -78 -16 0 Return Value is : 127

When you initialize the string in the definition part, the rest of the string is filled with 0 s. 初始化定义部分中的字符串时,字符串的其余部分将填充0秒。

But, when you just define it and then assign values to them, it is filled with junk values 但是,当你只是定义它然后为它们赋值时,它就会被垃圾值填充

Partially initialised array will filled or initiaslised with Zeroes of remaining uninitialised memory Partially initialised数组将使用剩余未初始化内存的零填充或初始化

eg 例如

int a[10]={1,2,3}

remaining index will be filled (initialised)with zeros(0) 剩余索引将用零(0)填充(初始化)

As already everybody has enlightened you with the correct answer. 已经每个人都用正确的答案启发了你。 This example is to make it more clear:: 这个例子是为了让它更清晰::

int main( int argc, char** argv )
{
 char temp[100] = {'A'} ;
 return 0 ;
}

The disassembly for array initializing part is:: 数组初始化部分的反汇编是::

//char temp[100] = {'A'} ;
// 41h is 'A', so this instruction fills the first index with 'A'
mov         byte ptr [ebp-6Ch],41h 
push        63h  
push        0    
lea         eax,[ebp-6Bh] 
push        eax 

// Then it calls memset which in turn fills the rest of array with zeroes.
call        @ILT+115(_memset) (0B11078h) 
add         esp,0Ch

Whether the scope of string2 array is global or local, it will be initialized to zero, in this case. 无论string2数组的范围是全局还是本地,在这种情况下,它都将初始化为零。

The only difference you will observe is when you declare string2 locally and do not initialize it at all, ie char string2[20]; 您将观察到的唯一区别是当您在本地声明string2并且根本没有初始化它时,即char string2[20]; .

Doing this way locally allocates string2 on stack and no explicit initilaization takes place, but if you do the same globally the array will be initialized to zero (all indices). 这样做就可以在堆栈上本地分配string2并且不会发生明确的初始化,但是如果你在全局做同样的事情,那么数组将被初始化为零(所有索引)。

Therefore, your guess is partially correct!! 因此,你的猜测是部分正确的!!

C11 (n1570), §7.24.2.3 The strcpy function C11(n1570),§7.24.2.3strcpy函数

The strcpy function copies the string pointed to by s2 ( including the terminating null character ) into the array pointed to by s1. strcpy函数将s2指向的字符串( 包括终止空字符 )复制到s1指向的数组中。

The first 6 elements in your string1 and string2 become the same. string1和string2中的前6个元素变得相同。 Terminator is also at the same position. 终结者也在同一个位置。

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

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