简体   繁体   English

这在c中意味着什么? char * array [] = {“**”,“**”,“**”};

[英]What does that mean in c? char *array[]= { “**”, “**”, “**” };

In some code that I read, there was an initializing statement like this 在我读到的一些代码中,有一个像这样的初始化语句

char *array[]= { "something1", "something2", "something3" };

What does this mean, and what does that pointer actually point to? 这意味着什么,该指针实际指向什么? How is that allocated in memory, and how can I access every element and every character of an element in that array ? 如何在内存中分配,以及如何访问该数组中元素的每个元素和每个字符?

--- Edited --- and please what is the difference in this example between char array[3]; ---编辑---请问这个例子在char数组之间有什么区别[3]; and char *array[3]; 和char * array [3]; --- Edited --- ---编辑---

what that means ? 那是什么意思 ?

It's initializing an array of strings ( char * ) with three values (three pointers to null-terminating strings) 它正在初始化一个包含三个值的字符串数组( char * )(三个指向空终止字符串的指针)

and what that pointer points to ? 那个指针指向什么?

It should point to the first element in the char* array 它应该指向char*数组中的第一个元素

how is that allocated in memory ? 怎么分配在内存中?

It will allocate enough memory to store the three strings followed by null-terminators, as well as the three pointers to those strings: 它将分配足够的内存来存储三个字符串,后跟null-terminators,以及指向这些字符串的三个指针:

array --> pointer to three sequential memory addresses

array[0] --> something1{\0}
array[1] --> something2{\0}
array[2] --> something3{\0}

Note that the strings may not necessarily be in sequential memory 请注意,字符串可能不一定在顺序存储器中

and how can I access every element 以及如何访问每个元素

if by "element" you mean the string, you can loop though the pointers: 如果用“element”表示字符串,你可以通过指针循环:

for(int i=0; i<3; i++)
{
    char* element = array[i];
}

and every character of an element in that array 以及该数组中元素的每个字符

well, you could access the chars using array syntax ( element[i] ) but I would recommend using the C string functions for safety (so you don't have to worry about accessing memory outside the range of the string) 好吧,您可以使用数组语法( element[i] )访问字符,但我建议使用C字符串函数以确保安全(因此您不必担心访问字符串范围之外的内存)

This is a way to initialize an array at the same time that you create it. 这是一种在创建数组的同时初始化数组的方法。

This code 这段代码

char *array[]= { "a", "b", "c" };

will have the same result as this code. 将与此代码具有相同的结果。

char *array[3];

array[0] = "a";
array[1] = "b";
array[2] = "c";

Here is a good source for more information. 这是获取更多信息的好来源。

http://www.iu.hio.no/~mark/CTutorial/CTutorial.html#Strings http://www.iu.hio.no/~mark/CTutorial/CTutorial.html#Strings

EDIT: 编辑:

char array[3]; is an array of 3 char . 是一个3 char的数组。 char *array[3]; is an array of 3 pointers to char . 是一个3个指向char的数组。

char * in C is a string. C中的char *是一个字符串。

array is the name of the variable being declared. array是要声明的变量的名称。

[] indicates that it is an array. []表示它是一个数组。

{ "something1", "something2", "something3" } is initializing the contents of the array. { "something1", "something2", "something3" }正在初始化数组的内容。

Accessing elements is done like so: 访问元素的方式如下:

array[0] gives the 1st element - "something1". array[0]给出第一个元素 - “something1”。

array[1] gives the 2nd element - "something2". array[1]给出第二个元素 - “something2”。

etc. 等等

Note: 注意:

As was pointed out in the comments, char * isn't technically a string. 正如评论中指出的那样, char *在技​​术上并不是一个字符串。

It's a pointer to a char . 它是一个指向char的指针。 You can visualize a string in memory like so: 您可以在内存中可视化字符串,如下所示:

<-------------------------->
..134|135|136|137|138|139|..
<-------------------------->
  'H'|'e'|'l'|'l'|'o'|'\0'
<-------------------------->

This block of memory (locations 134-139) is holding the string of characters. 该存储器块(位置134-139)保持字符串。

For example: 例如:

array[0] actually returns a pointer to the first character in "something1". array[0]实际上返回指向“something1”中第一个字符的指针。

You use the fact that the characters are sequentially in memory to access the rest of the string in various ways: 您可以使用以下事实:字符在内存中按顺序以各种方式访问​​字符串的其余部分:

/* ch points to the 's' */
char* ch = array[0];

/* ch2 points to the 'e' */
char* ch2 = ch + 3;

/* ch3 == 'e' */
char ch3 = *ch2;

/* ch4 == 'e' */
char ch4 = *(ch + 3);

/* ch5 == 'e' */
char ch5 = ch[3];

This defines a array of char pointers (aka. "c strings"). 这定义了一个char指针数组(又名“c strings”)。

For accessing the content you could do: 要访问您可以执行的内容:

for (int i=0; i<sizeof(array)/sizeof(char*); i++) {
    printf("%s", array[i]);
}

It declares array as an array of 3 pointers to char , with its 3 elements initialized to pointers to the respective strings. 它将array声明为一个包含3个指向char指针的数组,其3个元素初始化为指向相应字符串的指针。 The memory is allocated for the array itself (3 pointers) and for the strings. 内存是为数组本身(3个指针)和字符串分配的。 The strings memory is allocated statically. 字符串内存是静态分配的。 The array's memory is allocated either statically (if the declaration is outside of all functions) or dynamically (typically, on the execution stack of the CPU) if the declaration is inside a function. 如果声明在函数内部,则静态地(如果声明在所有函数之外)或动态(通常在CPU的执行堆栈上)分配数组的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM