LukasHeinzl
LukasHeinzl

Reputation: 51

Rename arrays in c

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!

Upvotes: 0

Views: 1455

Answers (2)

Andrew Henle
Andrew Henle

Reputation: 1

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.

Upvotes: 0

R Sahu
R Sahu

Reputation: 206637

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;

Upvotes: 3

Related Questions