简体   繁体   English

用字符串文字初始化的字符数组和使用strcpy的字符数组之间的区别

[英]difference between character array initialized with string literal and one using strcpy

Please help me understand what is the difference using an initialized character array like char line[80]="1:2" ( which doesn't work !! ) and using char line[80] followed by strcpy(line,"1:2") . 请帮助我了解使用诸如char line[80]="1:2"这样的初始化字符数组(不起作用!!)并使用char line[80]后跟strcpy(line,"1:2")
As per my understanding in first case I have a charachter array, it has been allocated memory, and i am copying a string literal to it. 根据我对第一种情况的理解,我有一个charachter数组,它已分配了内存,并且我正在向其中复制字符串文字。 And the same is done in the second case. 在第二种情况下也是如此。 But obviously I am wrong. 但是显然我错了。 So what is wrong with my understanding. 所以我的理解有什么问题。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void tokenize(char* line)
{
   char* cmd = strtok(line,":");

    while (cmd != NULL)
    {
    printf ("%s\n",cmd);
    cmd = strtok(NULL, ":");
    } 
}

int main(){
char line[80]; //char line[80]="1:2" won't work 
/*
char *line;
line = malloc(80*sizeof(char));
strcpy(line,"1:2");
*/
strcpy(line,"1:2");
tokenize(line);
return 0;
}

You are wrong. 你错了。 The result of these two code snippets 这两个代码段的结果

char line[80] = "1:2";

and

char line[80];
strcpy( line, "1:2" );

is the same. 是一样的 That is this statement 那就是这句话

char line[80] = "1:2";

does work.:) There is only one difference. 确实有效。:)只有一个区别。 When the array is initialized by the string literal all elements of the array that will not have a corresponding initialization character of the string literal will be initialized by '\\0' that is they will be zero-initialized. 当使用字符串文字初始化数组时,所有不具有字符串文字相应初始化字符的数组元素都将以'\\0'进行初始化,即它们将被零初始化。 While when function strcpy is used then the elements of the array that were not overwritten by characters of the string literal will have undeterminated values. 当使用函数strcpy时,未被字符串文字的字符覆盖的数组元素将具有未确定的值。

The array is initialized by the string literal (except the zero initialization of its "tail") the same way as if there was applied function strcpy . 数组由字符串文字(除了其“ tail”的零初始化除外)初始化,就像应用了函数strcpy

The exact equivalence of the result exists between these statements 这些语句之间存在精确的结果等价关系

char line[80] = "1:2";

and

char line[80];
strncpy( line, "1:2", sizeof( line ) );

that is when you use function strncpy and specify the size of the target array. 也就是说,当您使用函数strncpy并指定目标数组的大小时。

If you mean that you passed on to function tokenize directly the string literal as an argument of the function then the program has undefined behaviour because you may not change string literals. 如果您是要传递给函数直接将字符串文字tokenize为函数的参数,则该程序具有未定义的行为,因为您不能更改字符串文字。

According to the C Standard (6.4.5 String literals) 根据C标准(6.4.5字符串文字)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. 7不确定这些数组是否是唯一的,只要它们的元素具有适当的值即可。 If the program attempts to modify such an array, the behavior is undefined. 如果程序尝试修改这样的数组,则行为是不确定的。

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

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