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] };
This question is related to
c
The compilation problem only occurs for me (gcc 4.3, ubuntu 8.10) if the three variables are global. The problem is that C doesn't work like a script languages, so you cannot take for granted that the initialization of u and t occur after the one of s. That's why you get a compilation error. Now, you cannot initialize t and y they way you did it before, that's why you will need a char*. The code that do the work is the following:
#include <stdio.h>
#include <stdlib.h>
#define STR "ABCD"
const char s[] = STR;
char* t;
char* u;
void init(){
t = malloc(sizeof(STR)-1);
t[0] = s[0];
t[1] = s[1];
t[2] = s[2];
t[3] = s[3];
u = malloc(sizeof(STR)-1);
u[0] = s[3];
u[1] = s[2];
u[2] = s[1];
u[3] = s[0];
}
int main(void) {
init();
puts(t);
puts(u);
return EXIT_SUCCESS;
}
Perhaps your character array needs to be constant. Since you're initializing your array with characters from a constant string, your array needs to be constant. Try this:
#define S "ABCD"
const char a[] = { S[0], S[1], S[2], S[3] };
That's one of the cases a script to generate the appropriate code might help.
const char S[] = "ABCD";
This should work. i use this notation only and it works perfectly fine for me. I don't know how you are using.
I'm not sure what your problem is, but the following seems to work OK:
#include <stdio.h>
int main()
{
const char s0[] = "ABCD";
const char s1[] = { s0[3], s0[2], s0[1], s0[0], 0 };
puts(s0);
puts(s1);
return 0;
}
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
cl /Od /D "WIN32" /D "_CONSOLE" /Gm /EHsc /RTC1 /MLd /W3 /c /ZI /TC
.\Tmp.c
Tmp.c
Linking...
Build Time 0:02
C:\Tmp>tmp.exe
ABCD
DCBA
C:\Tmp>
Edit 9 June 2009
If you need global access, you might need something ugly like this:
#include <stdio.h>
const char *GetString(int bMunged)
{
static char s0[5] = "ABCD";
static char s1[5];
if (bMunged) {
if (!s1[0]) {
s1[0] = s0[3];
s1[1] = s0[2];
s1[2] = s0[1];
s1[3] = s0[0];
s1[4] = 0;
}
return s1;
} else {
return s0;
}
}
#define S0 GetString(0)
#define S1 GetString(1)
int main()
{
puts(S0);
puts(S1);
return 0;
}
This compiles fine on gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4).
const char s[] = "cheese";
int main()
{
return 0;
}
Here is obscure solution: define macro function:
#define Z(x) \
(x==0 ? 'A' : \
(x==1 ? 'B' : \
(x==2 ? 'C' : '\0')))
char x[] = { Z(0), Z(1), Z(2) };
Weird error.
Can you test this?
const char* const S = "ABCD";
char t[] = { S[0], S[1], S[2], S[3] };
char u[] = { S[3], S[2], S[1], S[0] };
Simply
const char S[] = "ABCD";
should work.
What's your compiler?
Another option is to use sprintf.
For example,
char buffer[50];
sprintf( buffer, "My String" );
Good luck.
Source: Stackoverflow.com