0

I would like to know if it is possible to give arrays in c aliases. My first try was this:

#define string char[]

But of course this does not work because array in c are defines like this:

 char test[] = ""; //Correct
 char[] test = ""; //Wrong

Do you know a workaround to this or is this not possible in standard c? Thanks in advance!

2 Answers2

3

You can use typedef.

typedef char string[];
string a = "abcd";

However, it is far from perfect. You cannot use it without an initializer when defining a variable. The following won't work.

string b;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Adding to @RSahu's answer, using #define for types is dangerous. For example:

#define STRING char *
STRING a, b;

See this answer for more details.

Community
  • 1
  • 1
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56