Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Initialize char array from a string

Tags:

c

I want to do the following

char a[] = { 'A', 'B', 'C', 'D'};

But I do not want to write these characters separately. I want something like

#define S "ABCD"

char a[] = { S[0], S[1], S[2], S[3] };

But this won't compile (gcc says 'initializer element is not constant').

I tried replacing the #define line with

const char S[] = "ABCD";

But that doesn't seem to help.

How can I do this (or something similar) that lets me write the "ABCD" as a normal 'string', and not as four separate characters?

P.S. It seems that people do not read the question correctly...

I can't get the following code to compile:

const char S[] = "ABCD";
char t[] = { S[0], S[1], S[2], S[3] };
char u[] = { S[3], S[2], S[1], S[0] };
like image 279
Arnaud Gouder de Beauregard Avatar asked Jun 08 '09 08:06

Arnaud Gouder de Beauregard


People also ask

How do you initialize an array of char type?

Initializing array with a string (Method 1):char arr[] = {'c','o','d','e','\0'}; In the above declaration/initialization, we have initialized array with a series of character followed by a '\0' (null) byte. The null byte is required as a terminating byte when string is read as a whole.

Can I use string as char array in Java?

char[] toCharArray() : This method converts string to character array. The char array size is same as the length of the string. char charAt(int index) : This method returns character at specific index of string.

How do you initialize a char array in Java?

A char array can be initialized by conferring to it a default size. char [] JavaCharArray = new char [ 4 ];


2 Answers

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.

like image 134
Johannes Schaub - litb Avatar answered Oct 20 '22 23:10

Johannes Schaub - litb


Simply

const char S[] = "ABCD";

should work.

What's your compiler?

like image 42
mmx Avatar answered Oct 21 '22 00:10

mmx