English 中文(简体)
A. 引进小公司
原标题:Initializing char Array with Smaller String Literal

如果我写:

char arr[8] = "abc";

Is there any specification over what arr[4] might be? I did some tests with Clang and it seems that the remaining chars in the array are set to null. Also, char arr[8] = ""; zeroes every byte. Not sure if this is a compiler convenience, standard behavior, pure coincidence or I got it wrong.


void a()
{
    char arr[8] = "abc";    /* breakpoint here, line 3 */
    strcpy(arr, "1234567");
}
int main()
{
    a();
    a();
    return 0;
}

夸张记录:

Breakpoint 1, a () at str.c:3
3           char arr[8] = "abc";
(gdb) s
Current language:  auto; currently minimal
4           strcpy(arr, "1234567");
(gdb) p arr
$1 = "abc00000000"
(gdb) c      
Continuing.

Breakpoint 1, a () at str.c:3
3           char arr[8] = "abc";
(gdb) p arr
$2 = "1234567"
(gdb) s
4           strcpy(arr, "1234567");
(gdb) p arr
$3 = "abc00000000"
最佳回答
问题回答
char arr[8] = "abc";

完全等于

char arr[8] = { a ,  b ,  c ,   };

ISO C 6.7.8 §21指出:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

In plain English this means that all values at the end of your array will be set to 0. So the standard guarantees that your code is equivalent to:

char arr[8] = { a ,  b ,  c ,   , 0, 0, 0, 0};

当然,的数值也是零。

This rule is universal to all arrays and not just to strings. Also, the same applies when initializing a struct but only explicitly setting a few of its members (6.7.8 §18).


This is why you can write code like

char arr[8] = "";

In this example, the first element of the array is initialized explicity to , and the rest of the items implicitly to zero. The compiler translates this to

char arr[8] = {0, 0, 0, 0, 0, 0, 0, 0};

这是标准行为。 未明确入门的阵列的每一部分,如果在声明中初步确定阵列的任何序号,则先入为违约值(<>code >。 它还从事其他类型的工作:

int a[10] = {1};

zeroes out a[1] through a[9].

根据该标准,除具体规定外的所有指数都将定为零/年。 https://stackoverflow.com/q/1352379/900873”





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签