C语言 C 中的二维字符数组初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6599707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:04:41  来源:igfitidea点击:

2D character array initialization in C

carraysmultidimensional-arraychar

提问by Derek

I am trying to build a list of strings that I need to pass to a function expecting char **

我正在尝试构建一个字符串列表,我需要将其传递给期望的函数 char **

How do I build this array? I want to pass in two options, each with less than 100 characters.

我如何构建这个数组?我想传入两个选项,每个选项都少于 100 个字符。

char **options[2][100];

options[0][0] = 'test1';
options[1][0] = 'test2';

This does not compile. What am I doing wrong exactly? How do I create a 2D character array in C?

这不编译。我到底做错了什么?如何在 C 中创建二维字符数组?

回答by Paul R

C strings are enclosed in double quotes:

C 字符串用双引号括起来:

const char *options[2][100];

options[0][0] = "test1";
options[1][0] = "test2";

Re-reading your question and comments though I'm guessing that what you reallywant to do is this:

重新阅读您的问题和评论,尽管我猜您真正想做的是:

const char *options[2] = { "test1", "test2" };

回答by Eric Leschinski

How to create an array size 5 containing pointers to characters:

如何创建一个大小为 5 的包含指向字符的指针的数组:

char *array_of_pointers[ 5 ];        //array size 5 containing pointers to char
char m = 'm';                        //character value holding the value 'm'
array_of_pointers[0] = &m;           //assign m ptr into the array position 0.
printf("%c", *array_of_pointers[0]); //get the value of the pointer to m

How to create a pointer to an array of characters:

如何创建指向字符数组的指针:

char (*pointer_to_array)[ 5 ];        //A pointer to an array containing 5 chars
char m = 'm';                         //character value holding the value 'm'
*pointer_to_array[0] = m;             //dereference array and put m in position 0
printf("%c", (*pointer_to_array)[0]); //dereference array and get position 0

How to create an 2D array containing pointers to characters:

如何创建包含指向字符的指针的二维数组:

char *array_of_pointers[5][2];          
//An array size 5 containing arrays size 2 containing pointers to char

char m = 'm';                           
//character value holding the value 'm'

array_of_pointers[4][1] = &m;           
//Get position 4 of array, then get position 1, then put m ptr in there.

printf("%c", *array_of_pointers[4][1]); 
//Get position 4 of array, then get position 1 and dereference it.

How to create a pointer to an 2D array of characters:

如何创建指向二维字符数组的指针:

char (*pointer_to_array)[5][2];
//A pointer to an array size 5 each containing arrays size 2 which hold chars

char m = 'm';                            
//character value holding the value 'm'

(*pointer_to_array)[4][1] = m;           
//dereference array, Get position 4, get position 1, put m there.

printf("%c", (*pointer_to_array)[4][1]); 
//dereference array, Get position 4, get position 1

To help you out with understanding how humans should read complex C/C++ declarations read this: http://www.programmerinterview.com/index.php/c-cplusplus/c-declarations/

为了帮助您理解人类应该如何阅读复杂的 C/C++ 声明,请阅读以下内容:http: //www.programmerinterview.com/index.php/c-cplusplus/c-declarations/

回答by Fred Foo

char **options[2][100];

declares a size-2 array of size-100 arrays of pointers to pointers to char. You'll want to remove one *. You'll also want to put your string literals in double quotes.

声明一个大小为 100 的指针数组的大小为 2 的数组,其中包含指向 的指针char。你会想要删除一个*。您还需要将字符串文字放在双引号中。

回答by Heath Hunnicutt

I think what you originally meant to do was to make an array only of characters, not of pointers:

我认为您最初的意图是制作一个仅包含字符而不是指针的数组:

char options[2][100];

options[0][0]='t';
options[0][1]='e';
options[0][2]='s';
options[0][3]='t';
options[0][4]='1';
options[0][5]='##代码##';  /* NUL termination of C string */

/* A standard C library function which copies strings. */
strcpy(options[1], "test2");

The code above shows two distinct methods of setting the character values in memory you have set aside to contain characters.

上面的代码显示了在内存中设置字符值的两种不同方法,这些方法是您预留的用于包含字符的。