2

What is the best way to initialize the string array in c?

I tried following two things

char arr[10] = "\0";
char arr1[10] = {"\0"};

After initializing those string, I tried to display in gdb, both gave the same initialization format.

(gdb) p arr
$1 = "\000\000\000\000\000\000\000\000\000"
(gdb) p arr1
$2 = "\000\000\000\000\000\000\000\000\000"
(gdb) 

I like to know which is best and what are the advantage and disadvantages.

Code:-

int main(){
    char arr[10] = "\0";
    char arr1[10] = {"\0"};

return 0;
}

Assembly:-

(gdb) disass main
Dump of assembler code for function main:

0x00000000004004ec <+0>:    push   %rbp
0x00000000004004ed <+1>:    mov    %rsp,%rbp
0x00000000004004f0 <+4>:    movzwl 0xd5(%rip),%eax        # 0x4005cc
0x00000000004004f7 <+11>:   mov    %ax,-0x10(%rbp)
0x00000000004004fb <+15>:   movq   $0x0,-0xe(%rbp)
0x0000000000400503 <+23>:   movzwl 0xc2(%rip),%eax        # 0x4005cc
0x000000000040050a <+30>:   mov    %ax,-0x20(%rbp)
0x000000000040050e <+34>:   movq   $0x0,-0x1e(%rbp)
0x0000000000400516 <+42>:   mov    $0x0,%eax
0x000000000040051b <+47>:   pop    %rbp
0x000000000040051c <+48>:   retq   
6
  • 1
    Correct me if I'm wrong, but AFAIK they're equivalent. Commented Mar 5, 2013 at 10:50
  • did you compare the disassembly , and see any difference at that level ? Commented Mar 5, 2013 at 10:50
  • @BarathBushan I am not good at assembly. Let me post my assembly code as well. Commented Mar 5, 2013 at 10:55
  • That is not string array, that is char array, also known as "string" in C. String array is a bit vague in C, but usually it means array of char pointers, like argv argument of main function: char *argv[] Commented Mar 5, 2013 at 11:06
  • Also, in C, literal string "" includes terminating '\0' char, in other words, in C empty string is char array with length 1, containing just byte 0. So "\0" is actually a char array of length 2, both 0 (string length is still 0). Commented Mar 5, 2013 at 11:09

4 Answers 4

7

Both initializations are completely equivalent. From the standard:

An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces.

Sign up to request clarification or add additional context in comments.

1 Comment

@Bingo: yes. I like to call {0} the "universal zero-initializer". It works for everything.
3

Your two initializations are equivalent.

Apart from what you had shown there are several ways which a string (not only [] array) can be initialized:

// fixed size, depending on the lenght of the string, no memory "wasted"
char arr1[] = "value";

// fixed array size, depends on a given number, some memory may be unused
char arr2[10] = "value";

// C-array type initialiation
char arr3[] = {'v','a','l','u','e','\0'};

// special string, should never be modified, need not be freed
char* str1 = "value";

// a dynamic string based on a constant value; has to be freed, but can be reallocated at will
char* str2 = strdup("value");

3 Comments

Of course it would! noone seems to do it though and I wanted to stress that it should not be modified even without const
strdup() is not described by the C Standard (it is POSIX though). Maybe better to malloc() and strcpy().
maybe it is (though I can't imagine life withouth strdup), but then it's quite long and unintuitive; strdup does it all. If you don't have it, you'll want to implement it anyway.
1

char arr[10] = "\0"; and char arr1[10] = {"\0"}; are equal.

Comments

1

The statements

char arr[10] = "\0";

and

char arr1[10] = {"\0"};

are exactly the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.