char数组的指针数组

时间:2022-02-20 21:17:04

I am confused about the following line of code:

我对以下代码行感到困惑:

char* words[] = { "aaa", "bbbb", "ccccc", "dddddd" };

The way I understand, each word is first stored and then each position of the array words will point then to the first character of each word. How are these strings stored? Is there dynamic allocation going on here or are these words stored on the stack?

我理解的方式是,每个单词首先被存储,然后数组单词的每个位置将指向每个单词的第一个字符。这些字符串是如何存储的?这里是否有动态分配,或者这些单词是否存储在堆栈中?

If they are stored on the stack, in which way are they stored? For instance, if I print some of the contents of words as below:

如果它们存储在堆栈中,它们以哪种方式存储?例如,如果我打印一些单词的内容如下:

#include <stdio.h>
int main () {
    char* words[] = { "aaa", "bbbb", "ccccc", "dddddd" };
    printf("\n\n(*words)[0] = %s", words[0]);
    printf("\n\n(*words)[0]+1 = %s", words[0]+1);
    return 0;
}

instead of printing aaa and bbbb, what I get is aaa and aa. I do not really understand what is the reason for this since, the way I see it, words[0]+1 should point to the string bbbb and not to the second character of aaa. What is going on here?

而不是打印aaa和bbbb,我得到的是aaa和aa。我真的不明白这是什么原因,因为我看到它的方式,单词[0] +1应该指向字符串bbbb而不指向aaa的第二个字符。这里发生了什么?

8 个解决方案

#1


15  

this is like..

这就像......

char数组的指针数组

since words an array of character of pointers so each array index of words will hold the address of the string literal ,i.e. base address of the string literals if you will print

因为单词指针的字符数组所以每个单词的数组索引将保存字符串文字的地址,即。如果要打印,则为字符串文字的基址

 printf("%s",words[0])// index 0 will print aaa.

#2


6  

The difference is because words[0]+1 is not the same as words[0+1].

区别是因为单词[0] +1与单词[0 + 1]不同。

The former points to the second character in words[0], while the latter points to the second word.

前者指向单词[0]中的第二个字符,而后者指向第二个单词。

#3


3  

words[0] points to the first 'a' in "aaa".

单词[0]指向“aaa”中的第一个“a”。

words[0]+1 moves that pointer along by one character, so it comes to point at the second 'a'.

words [0] +1将指针移动一个字符,因此它指向第二个'a'。

words[1] points at "bbbb"

单词[1]指向“bbbb”

words[1]+1 points "bbb", e.g. the second 'b' in "bbbb".

单词[1] +1分“bbb”,例如“bbbb”中的第二个“b”。

#4


2  

the way I see it, words[0]+1 should point to the string bbbb and not to the second character of aaa

我看到它的方式,单词[0] +1应指向字符串bbbb而不指向aaa的第二个字符

No. words[0] is a char pointer itself - it's perfectly fine that you get the second character of "aaa" by adding one to it. What you want is words + 1 or &words[0] + 1 which will correctly be "bbbb".

字数[0]本身就是一个字符指针 - 你可以通过添加一个来获得“aaa”的第二个字符。你想要的是单词+ 1或&words [0] + 1,它们将正确地为“bbbb”。

Also, the strings themselves are allocated upon launching the executable and they're probably put in the data or bss section of the binary by the linker. Also, when you declare and initialize words, it'll be allocated on the stack as any other automatic array and its items will be assigned to pointers to the beginning of each string constant.

此外,字符串本身在启动可执行文件时分配,它们可能被链接器放入二进制文件的数据或bss部分。此外,当您声明和初始化单词时,它将作为任何其他自动数组分配在堆栈上,并且其项目将分配给指向每个字符串常量开头的指针。

#5


1  

The literal strings are stored in static memory. Their actual location is implementation-dependent but literal strings are stored somewhere, typically in the data portion of the executable - this is neither dynamic nor stack allocation. Your array then contains pointers to these locations.

文字字符串存储在静态存储器中。它们的实际位置是依赖于实现的,但文字字符串存储在某处,通常存储在可执行文件的数据部分中 - 这既不是动态也不是堆栈分配。然后,您的数组包含指向这些位置的指针。

words[0]+1 should should point to the string bbbb and not to the second character of aaa.

单词[0] +1应该指向字符串bbbb而不是指向aaa的第二个字符。

This simply isn't how array indexing works. You index the array of strings with words[0], now you have a string, and any operations apply to that string. You can't do arithmetic with array indices outside the subscript. To get to the string "bbbb", you would use words[1].

这根本不是数组索引的工作原理。使用单词[0]索引字符串数组,现在您有一个字符串,并且任何操作都适用于该字符串。你不能对下标之外的数组索引进行算术运算。要获得字符串“bbbb”,您将使用单词[1]。

#6


1  

Both stack and heap space is dynamically allocated -- that is, they are allocated at run time. Is your compiled code dynamically allocated, on the heap or stack? Obviously neither. Storage for constants is similar to storage for code ... they are stored in the executable on disk and loaded into read-only memory. (Note for pedants: this is how things are done in a typical implementation; it is not mandated by the language standard.)

堆栈和堆空间都是动态分配的 - 也就是说,它们是在运行时分配的。您的编译代码是在堆还是堆栈上动态分配的?显然都没有。常量存储类似于代码存储...它们存储在磁盘上的可执行文件中并加载到只读存储器中。 (关于学生的注意事项:这是在典型的实现中完成的事情;它不是语言标准规定的。)

words[0] is the address of the first 'a' of "aaaa". Adding 1 to that address should surely yield the address of the second 'a' of "aaaa". The address of "bbbb" is way over at words[1].

words [0]是“aaaa”的第一个'a'的地址。向该地址添加1肯定会产生“aaaa”的第二个“a”的地址。 “bbbb”的地址在单词[1]上结束了。

In the format of your printf you have "(*words)[0]", but that's different. *words is the same as words[0]. (*words)[0] is the same as **words, which is the first 'a' (not its address) of "aaaa". You would print (*words)[0] with %c, not %s.

在你的printf格式中你有“(* words)[0]”,但那是不同的。 *单词与单词[0]相同。 (*单词)[0]与**单词相同,这是“aaaa”的第一个'a'(不是它的地址)。你会打印(* words)[0]%c,而不是%s。

#7


0  

words[0] returns the address of aaa. Adding 1 to that increments the address to point to the second a.

words [0]返回aaa的地址。添加1会增加地址以指向第二个a。

Do you mean to have words[0+1]?

你的意思是说[0 + 1]吗?

That should give you what you are expecting.

这应该会给你你所期待的。

#8


0  

char* words[] = { "aaa", "bbbb", "ccccc", "dddddd" };

All the 4 strings have static storage duration and are allocated prior to the program startup.

所有4个字符串都具有静态存储持续时间,并在程序启动之前分配。

In the initializer, the arrays are converted to pointers to char and the words array is initialized with the pointer values.

在初始化程序中,数组转换为指向char的指针,并使用指针值初始化单词数组。

#1


15  

this is like..

这就像......

char数组的指针数组

since words an array of character of pointers so each array index of words will hold the address of the string literal ,i.e. base address of the string literals if you will print

因为单词指针的字符数组所以每个单词的数组索引将保存字符串文字的地址,即。如果要打印,则为字符串文字的基址

 printf("%s",words[0])// index 0 will print aaa.

#2


6  

The difference is because words[0]+1 is not the same as words[0+1].

区别是因为单词[0] +1与单词[0 + 1]不同。

The former points to the second character in words[0], while the latter points to the second word.

前者指向单词[0]中的第二个字符,而后者指向第二个单词。

#3


3  

words[0] points to the first 'a' in "aaa".

单词[0]指向“aaa”中的第一个“a”。

words[0]+1 moves that pointer along by one character, so it comes to point at the second 'a'.

words [0] +1将指针移动一个字符,因此它指向第二个'a'。

words[1] points at "bbbb"

单词[1]指向“bbbb”

words[1]+1 points "bbb", e.g. the second 'b' in "bbbb".

单词[1] +1分“bbb”,例如“bbbb”中的第二个“b”。

#4


2  

the way I see it, words[0]+1 should point to the string bbbb and not to the second character of aaa

我看到它的方式,单词[0] +1应指向字符串bbbb而不指向aaa的第二个字符

No. words[0] is a char pointer itself - it's perfectly fine that you get the second character of "aaa" by adding one to it. What you want is words + 1 or &words[0] + 1 which will correctly be "bbbb".

字数[0]本身就是一个字符指针 - 你可以通过添加一个来获得“aaa”的第二个字符。你想要的是单词+ 1或&words [0] + 1,它们将正确地为“bbbb”。

Also, the strings themselves are allocated upon launching the executable and they're probably put in the data or bss section of the binary by the linker. Also, when you declare and initialize words, it'll be allocated on the stack as any other automatic array and its items will be assigned to pointers to the beginning of each string constant.

此外,字符串本身在启动可执行文件时分配,它们可能被链接器放入二进制文件的数据或bss部分。此外,当您声明和初始化单词时,它将作为任何其他自动数组分配在堆栈上,并且其项目将分配给指向每个字符串常量开头的指针。

#5


1  

The literal strings are stored in static memory. Their actual location is implementation-dependent but literal strings are stored somewhere, typically in the data portion of the executable - this is neither dynamic nor stack allocation. Your array then contains pointers to these locations.

文字字符串存储在静态存储器中。它们的实际位置是依赖于实现的,但文字字符串存储在某处,通常存储在可执行文件的数据部分中 - 这既不是动态也不是堆栈分配。然后,您的数组包含指向这些位置的指针。

words[0]+1 should should point to the string bbbb and not to the second character of aaa.

单词[0] +1应该指向字符串bbbb而不是指向aaa的第二个字符。

This simply isn't how array indexing works. You index the array of strings with words[0], now you have a string, and any operations apply to that string. You can't do arithmetic with array indices outside the subscript. To get to the string "bbbb", you would use words[1].

这根本不是数组索引的工作原理。使用单词[0]索引字符串数组,现在您有一个字符串,并且任何操作都适用于该字符串。你不能对下标之外的数组索引进行算术运算。要获得字符串“bbbb”,您将使用单词[1]。

#6


1  

Both stack and heap space is dynamically allocated -- that is, they are allocated at run time. Is your compiled code dynamically allocated, on the heap or stack? Obviously neither. Storage for constants is similar to storage for code ... they are stored in the executable on disk and loaded into read-only memory. (Note for pedants: this is how things are done in a typical implementation; it is not mandated by the language standard.)

堆栈和堆空间都是动态分配的 - 也就是说,它们是在运行时分配的。您的编译代码是在堆还是堆栈上动态分配的?显然都没有。常量存储类似于代码存储...它们存储在磁盘上的可执行文件中并加载到只读存储器中。 (关于学生的注意事项:这是在典型的实现中完成的事情;它不是语言标准规定的。)

words[0] is the address of the first 'a' of "aaaa". Adding 1 to that address should surely yield the address of the second 'a' of "aaaa". The address of "bbbb" is way over at words[1].

words [0]是“aaaa”的第一个'a'的地址。向该地址添加1肯定会产生“aaaa”的第二个“a”的地址。 “bbbb”的地址在单词[1]上结束了。

In the format of your printf you have "(*words)[0]", but that's different. *words is the same as words[0]. (*words)[0] is the same as **words, which is the first 'a' (not its address) of "aaaa". You would print (*words)[0] with %c, not %s.

在你的printf格式中你有“(* words)[0]”,但那是不同的。 *单词与单词[0]相同。 (*单词)[0]与**单词相同,这是“aaaa”的第一个'a'(不是它的地址)。你会打印(* words)[0]%c,而不是%s。

#7


0  

words[0] returns the address of aaa. Adding 1 to that increments the address to point to the second a.

words [0]返回aaa的地址。添加1会增加地址以指向第二个a。

Do you mean to have words[0+1]?

你的意思是说[0 + 1]吗?

That should give you what you are expecting.

这应该会给你你所期待的。

#8


0  

char* words[] = { "aaa", "bbbb", "ccccc", "dddddd" };

All the 4 strings have static storage duration and are allocated prior to the program startup.

所有4个字符串都具有静态存储持续时间,并在程序启动之前分配。

In the initializer, the arrays are converted to pointers to char and the words array is initialized with the pointer values.

在初始化程序中,数组转换为指向char的指针,并使用指针值初始化单词数组。