How do I create an array of strings in C?

前端 未结 14 2672
野的像风
野的像风 2020-11-22 14:43

I am trying to create an array of strings in C. If I use this code:

char (*a[2])[14];
a[0]=\"blah\";
a[1]=\"hmm\";

gcc gives me \"warning:

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 15:33

    If the strings are static, you're best off with:

    const char *my_array[] = {"eenie","meenie","miney"};
    

    While not part of basic ANSI C, chances are your environment supports the syntax. These strings are immutable (read-only), and thus in many environments use less overhead than dynamically building a string array.

    For example in small micro-controller projects, this syntax uses program memory rather than (usually) more precious ram memory. AVR-C is an example environment supporting this syntax, but so do most of the other ones.

提交回复
热议问题