Am I correct in that the above fails because I'm telling it to put "Mary" in the name[20] element? Technically correct. name[20] is actually out of bounds due to 0-based indexing, so it would be the 21st element in a 20 element array. Either way, you're trying to assign a char* to a char. Is there a way to initialize char name[20] array separately from the declaration without using srtcpy()? Not with a literal. You have to manually copy the bytes. The string literal initialization is a unique exception granted by the standard to char[] objects; normally a string literal is converted to a pointer, but when it appears as an initializer for a char[] it gets copied into the array. Answer from glasket_ on reddit.com
🌐
Reddit
reddit.com › r/cprogramming › initialize a char array?
r/cprogramming on Reddit: Initialize a char array?
April 23, 2024 -

How come this works:

Char name[20] = "Mary";

But this doesn't.

Char name[20];

Name[20] = "Mary";

Am I correct in that the above fails because I'm telling it to put "Mary" in the name[20] element? Is that how it's interpreted?

Is there a way to initialize char name[20] array separately from the declaration without using srtcpy()? Just wondering

Thanks

🌐
Quora
quora.com › How-do-I-initialize-a-char-array-in-C
How to initialize a char array in C - Quora
Answer (1 of 11): To initialize a char array in C, you can use either of the following methods: 1. Initializing at the time of declaration: > [code]char str[] = "Hello, World!"; [/code] In this method, the size of the array is automatically determined by the size of the string literal. 2. Ini...
Discussions

C char array initialization: what happens if there are less characters in the string literal than the array size? - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'm not sure what will be in the char array after initialization in the ... More on stackoverflow.com
🌐 stackoverflow.com
c - Is initializing a char[] with a string literal bad practice? - Software Engineering Stack Exchange
In the first case, the size of the array is taken from the size of the initializer. String literals are stored as arrays of char with a terminating 0 byte, so the size of the array is 8 ('o', 'c', 't', 'o', 'b', 'e', 'r', 0). More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
January 16, 2013
How to initialize a char array with double quotes (not printable escape characters) in C++
Hi, I am trying to initialize a character array with double quotes inside of the array. I am never going to print the array out, so I do not need the printable escape characcter \". I want to be able to read and compare the double quote inside of the array in the program. More on learn.microsoft.com
🌐 learn.microsoft.com
2
0
February 3, 2023
Initializing Char Arrays - C++ Forum
Hi, I wrote a simple program that takes a users information and displays it back to the user. For my char arrays I ran a forloop and initialized them to blank spaces(" "). I know that you can also initialize to ('\n') or NULL. I just wanted to know what is the best option to use. More on cplusplus.com
🌐 cplusplus.com
June 8, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-initialize-char-array-in-struct-in-c
How to Initialize Char Array in Struct in C? - GeeksforGeeks
July 23, 2025 - When we create a structure instance (variable) in C that includes a char array, we can set an initial value for that char array directly using the list initialization.
🌐
Delft Stack
delftstack.com › home › howto › initialize char array in c
How to Initialize Char Array in C | Delft Stack
February 2, 2024 - In this case, we declare a 5x5 char array and include five braced strings inside the outer curly braces. Note that each string literal in this example initializes the five-element rows of the matrix.
🌐
Sanfoundry
sanfoundry.com › c-tutorials-character-array-initialization
Character Array Initialization in C - Sanfoundry
December 31, 2025 - This program defines a character array portal and initializes it with the string “Sanfoundry.” It then prints the string using the printf() function.
🌐
IBM
ibm.com › docs › en › i › 7.4.0
Initialization of character arrays
We cannot provide a description for this page right now
Find elsewhere
Top answer
1 of 6
70

It's anyways bad practice to initialie a char array with a string literal.

The author of that comment never really justifies it, and I find the statement puzzling.

In C (and you've tagged this as C), that's pretty much the only way to initialize an array of char with a string value (initialization is different from assignment). You can write either

char string[] = "october";

or

char string[8] = "october";

or

char string[MAX_MONTH_LENGTH] = "october";

In the first case, the size of the array is taken from the size of the initializer. String literals are stored as arrays of char with a terminating 0 byte, so the size of the array is 8 ('o', 'c', 't', 'o', 'b', 'e', 'r', 0). In the second two cases, the size of the array is specified as part of the declaration (8 and MAX_MONTH_LENGTH, whatever that happens to be).

What you cannot do is write something like

char string[];
string = "october";

or

char string[8];
string = "october";

etc. In the first case, the declaration of string is incomplete because no array size has been specified and there's no initializer to take the size from. In both cases, the = won't work because a) an array expression such as string may not be the target of an assignment and b) the = operator isn't defined to copy the contents of one array to another anyway.

By that same token, you can't write

char string[] = foo;

where foo is another array of char. This form of initialization will only work with string literals.

EDIT

I should amend this to say that you can also initialize arrays to hold a string with an array-style initializer, like

char string[] = {'o', 'c', 't', 'o', 'b', 'e', 'r', 0};

or

char string[] = {111, 99, 116, 111, 98, 101, 114, 0}; // assumes ASCII

but it's easier on the eyes to use string literals.

EDIT2

In order to assign the contents of an array outside of a declaration, you would need to use either strcpy/strncpy (for 0-terminated strings) or memcpy (for any other type of array):

if (sizeof string > strlen("october"))
  strcpy(string, "october");

or

strncpy(string, "october", sizeof string); // only copies as many characters as will
                                           // fit in the target buffer; 0 terminator
                                           // may not be copied, but the buffer is
                                           // uselessly completely zeroed if the
                                           // string is shorter!
2 of 6
12

The only problem I recall is assigning string literal to char *:

char var1[] = "september";
var1[0] = 'S'; // Ok - 10 element char array allocated on stack
char const *var2 = "september";
var2[0] = 'S'; // Compile time error - pointer to constant string
char *var3 = "september";
var3[0] = 'S'; // Modifying some memory - which may result in modifying... something or crash

For example take this program:

#include <stdio.h>

int main() {
  char *var1 = "september";
  char *var2 = "september";
  var1[0] = 'S';
  printf("%s\n", var2);
}

This on my platform (Linux) crashes as it tries to write to page marked as read-only. On other platforms it might print 'September' etc.

That said - initialization by literal makes the specific amount of reservation so this won't work:

char buf[] = "May";
strncpy(buf, "September", sizeof(buf)); // Result "Sep"

But this will

char buf[32] = "May";
strncpy(buf, "September", sizeof(buf));

As last remark - I wouldn't use strcpy at all:

char buf[8];
strcpy(buf, "very long string very long string"); // Oops. We overwrite some random memory

While some compilers can change it into safe call strncpy is much safer:

char buf[1024];
strncpy(buf, something_else, sizeof(buf)); // Copies at most sizeof(buf) chars so there is no possibility of buffer overrun. Please note that sizeof(buf) works for arrays but NOT pointers.
buf[sizeof(buf) - 1] = '\0';
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1167546 › how-to-initialize-a-char-array-with-double-quotes
How to initialize a char array with double quotes (not printable escape characters) in C++ - Microsoft Q&A
February 3, 2023 - The char array is very large so initializing the char array like this will take up to much time: char hello[6] = { 'h', 'e', 'l', 'l', 'o', '/0' }; Here is a small portion of the character array I am trying to initialize: char BankgothicFont[] = { "char id = "39" x = "252" y = "70" width = "3" height = "7" xoffset = "1" yoffset = "2" >" "<char id = "40" x = "58" y = "49" width = "6" height = "12" xoffset = "2" yoffset = "5" >" "<char id = "41" x = "65" y = "48" width = "6" height = "12" xoffset = "1" yoffset = "5" >" "<char id = "42" x = "94" y = "109" width = "8" height = "8" xoffset = "2" yo
🌐
Cppreference
en.cppreference.com › w › c › language › array_initialization.html
Array initialization - cppreference.com
October 16, 2022 - 1) string literal initializer for character and wide character arrays · 2) comma-separated list of constant(until C99) expressions that are initializers for array elements, optionally using array designators of the form [ constant-expression ] = (since C99)
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › c › STR11-C.+Do+not+specify+the+bound+of+a+character+array+initialized+with+a+string+literal
STR11-C. Do not specify the bound of a character array initialized with a string literal - SEI CERT C Coding Standard - Confluence
Subclause 6.7.9, paragraph 14, of the C Standard [ISO/IEC 9899:2011], says: An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces.
🌐
Florida State University
cs.fsu.edu › ~myers › cgs3408 › notes › arrays.html
Array Basics
The input is stored in the character array and the null character is automatically appended. Note also that the & was not needed in the scanf call (word1 was used, instead of &word1). This is because the name of the array by itself (with no index) actually IS a variable that stores an address (a pointer). &nbsp; arrayinit.c -- example of array declarations and initializer ...
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 136005-help-initializing-char-array.html
help with initializing a char array
March 21, 2011 - People typically use memset for this in C. I guess it kinda depends on what you want to initialise it with though. My homepage Advice: Take only as directed - If symptoms persist, please see your debugger Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong" ... Sure it is. People typically use memset for this ...which uses a loop. You aren't using a loop directly, but the function you are calling does.
🌐
Scaler
scaler.com › home › topics › what is character array in c?
What is Character Array in C? - Scaler Topics
October 10, 2025 - The strlen() function is present in the string.h header and is used to find the length of a string. It has the following syntax, This function returns the length of the string. A easy way to initialize a character array is using string assignment.
🌐
Cplusplus
cplusplus.com › forum › general › 83475
Initializing Char Arrays - C++ Forum
June 8, 2022 - See here: http://www.cplusplus.com/doc/tutorial/arrays/ Wazzak ... No this initialize the first element to 'a' and the rest of the elements to '\0'.
Top answer
1 of 12
20

You can't - in C. In C initializing of global and local static variables are designed such that the compiler can put the values statically into the executable. It can't handle non-constant expressions as initializers. And only in C99, you can use non-constant expression in aggregate initializers - not so in C89!

In your case, since your array is an array containing characters, each element has to be an arithmetic constant expression. Look what it says about those

An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions.

Surely this is not satisfied by your initializer, which uses an operand of pointer type. Surely, the other way is to initialize your array using a string literal, as it explains too

All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

All quotes are taken out of the C99 TC3 committee draft. So to conclude, what you want to do - using non-constant expression - can't be done with C. You have several options:

  • Write your stuff multiple times - one time reversed, and the other time not reversed.
  • Change the language - C++ can do that all.
  • If you really want to do that stuff, use an array of char const* instead

Here is what i mean by the last option

char const c[] = "ABCD";
char const *f[] = { &c[0], &c[1], &c[2], &c[3] };
char const *g[] = { &c[3], &c[2], &c[1], &c[0] };

That works fine, as an address constant expression is used to initialize the pointers

An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; it shall be created explicitly using the unary & operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type. The array-subscript [] and member-access . and -> operators, the address & and indirection * unary operators, and pointer casts may be used in the creation of an address constant, but the value of an object shall not be accessed by use of these operators.

You may have luck tweaking your compiler options - another quote:

An implementation may accept other forms of constant expressions.

2 of 12
8

Simply

const char S[] = "ABCD";

should work.

What's your compiler?

🌐
Jeremy Lindsay
jeremylindsayni.wordpress.com › 2017 › 10 › 07 › two-ways-to-initialize-an-array-in-c
Two ways to initialize an array in C | Jeremy Lindsay
October 7, 2017 - But the technique of processing an array in C by passing a pointer to the start of the array is useful too. Pingback: Dew Drop - October 9, 2017 (#2577) - Morning Dew · Why the need for pointerToRadioTextData? It’s been a while since I coded in C, but I’m pretty sure ... Thank you James – this is a really good tip. I’ll test and if it works with Arduino I’ll update the article title to be three different ways to initialise an array (and give you credit of course).
🌐
Cplusplus
cplusplus.com › forum › general › 264314
In C++ 11, how can we initialize a char* - C++ Forum
char * b = &s[0]; //s is string. this works, but its not useful. s may not have a zero where you want it for c-string, because string works off the length not a terminal. if you modify b's data, and change the length, you will have bad problems. this may be what you wanted to express, but don't ...
🌐
Studytonight
studytonight.com › c › string-and-character-array.php
String and Character Arrays in C Language | Studytonight
Learn how to create a string, character arrays in C, string input and output and string functions in C.