Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways to initialize char array, are they correct?

I am using two type of declaration for char array in C as:

  1. char buf[BUFFLEN] = "Hello, I am client :)";

  2. char buf[BUFFLEN]; buf[BUFFLEN] = "Hello, I am client :)";

Where BUFFLEN being some constant.

When I compiled the second way, I get warning but no error as

warning: assignment makes integer from pointer without a cast

Can someone please explain me a difference. I checked few posts on it but really having hard time in understanding it completely.

like image 748
Jeetendra Ahuja Avatar asked Jun 24 '26 09:06

Jeetendra Ahuja


2 Answers

TL;DR they both are not initialization, the first one is initialization, the second one is (an attempt to) assignment.


The first case, is an attempt to define a char array and initialize the contents with the string literal "Hello, I am client :)". The syntax

 char buf[BUFFLEN] = "Hello, I am client :)";

is good, as we're allowed ## Note 1 to supply an initializer in form of a string literal to a char array. This snippet is a valid one, so we're good there.

However, in the second case,

  char buf[BUFFLEN];
  buf[BUFFLEN] = "Hello, I am client :)";

the second statement is wrong, as

  • buf[BUFFLEN] is out of bound memory access, it's off-by-one. This actually causes undefined behavior.

    -- FWIW, C uses 0-based indexing for arrays, so for an array defined with size BUFFLEN will have valid indexes ranging from 0 to BUFFLEN-1.

  • (considering) The syntax buf[BUFFLEN - <whatever>] = "some string"; is also invalid, as this is a constraint violation ## Note 2 . Check the data types.

    -- The LHS is of type char, you're trying to assign a value of type char [] (which decays to char*) and the assignment is invalid C. You need to copy the content, make use of strcpy() or alike.


## Note 1:

Quoting C11, chapter §6.7.9

An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

## Note 2:

C11, chapter §6.5.16.1, Simple assignment Constraints

One of the following shall hold:112)

  • the left operand has atomic, qualified, or unqualified arithmetic type, and the right has arithmetic type;

  • the left operand has an atomic, qualified, or unqualified version of a structure or union type compatible with the type of the right;

  • the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

  • the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) one operand is a pointer to an object type, and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

  • the left operand is an atomic, qualified, or unqualified pointer, and the right is a null pointer constant; or

  • the left operand has type atomic, qualified, or unqualified _Bool, and the right is a pointer.

like image 124
Sourav Ghosh Avatar answered Jun 26 '26 02:06

Sourav Ghosh


char buf[BUFFLEN] = "Hello, I am client :)";

This creates a character array and initializes it with the contents of the string literal.

char buf[BUFFLEN];

buf[BUFFLEN] = "Hello, I am client :)";

This creates an uninitialized character array, and then preforms an out of bounds access. The behavior of this snippet is undefined.

like image 31
StoryTeller - Unslander Monica Avatar answered Jun 26 '26 00:06

StoryTeller - Unslander Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!