Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

c - 将全大写变量传递给C中的const char数组指针(Passing an all-caps variable to a const char array pointer in C)

A const char * array in C is often formatted like this:

(C语言中的const char *数组通常采用以下格式:)

const char *my_array[] = {
   "array1",
   "array2",
   "array3"
};

Senior C programmers often pass an all-caps variable to the array like this:

(高级C程序员经常将全大写变量传递给数组,如下所示:)

const char *their_array[SOME_VARIABLE] = {
    "ARRAY_1",
    "ARRAY_2",
    "ARRAY_3"
};

What is the all-caps variable?

(什么是全大写变量?)

What are the advantages or disadvantages of using it?

(使用它的优缺点是什么?)

Thank you

(谢谢)

  ask by tilikoom translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Specifying he size in that case doesn't really matter, the compiler can infer the size from the initialization.

(在这种情况下,指定大小实际上并不重要,编译器可以从初始化中推断出大小。)

However, static keyword makes a different.

(但是,static关键字会有所不同。)

You can think of static as if the variable was global, but is only accessible from its scope.

(您可以将static视为全局变量,但只能从其范围访问。)

(Btw, I think you're missing an equal sign in your code)

((顺便说一句,我认为您在代码中缺少等号))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...