C++ 遍历 const char* 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12856845/
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
Iterating through const char* array
提问by cybercow
I have an really simple example of array of const char's and one function supposed to print them out (iterate through the chosen one). Contrary all my expectations, it's iterating through all of them and not only the one that was passed as argument.
我有一个非常简单的 const char 数组示例和一个应该将它们打印出来的函数(遍历所选的一个)。与我所有的期望相反,它正在迭代所有这些,而不仅仅是作为参数传递的那个。
#include <iostream>
const char* oranges[] = {
"ORANGE",
"RED ORANGE"
};
const char* apples[] = {
"APPLE"
};
const char* lemons[] = {
"LEMON"
};
void printFruit(const char** fruit){
int i =0;
while (fruit[i] != '---------------------
ORANGE
---------------------
RED ORANGE
---------------------
APPLE
---------------------
LEMON
'){
std::cout << "---------------------\n";
std::cout << fruit[i] << "\n";
i++;
}
}
int main (int argc, const char * argv[])
{
printFruit(oranges);
return 0;
}
The result i would expect is that the function printFruit with oranges given as argument will print ORANGE and RED ORANGE, meanwhile i get printed ALL of the fruits defined (from other arrays), like this:
我期望的结果是,函数 printFruit 与作为参数给出的橙子将打印 ORANGE 和 RED ORANGE,同时我打印了所有定义的水果(来自其他数组),如下所示:
while (fruit[i] != 'const char* oranges[] = {
"ORANGE",
"RED ORANGE",
0
};
')
Sorry for my ignorance but why is this happening ?
抱歉我的无知,但为什么会发生这种情况?
Edit: I followed this question: defining and iterating through array of strings in cthat is similar to mine.
回答by SingerOfTheFall
You are having UB here. Your condition
你在这里有UB。你的情况
const char* oranges[] = {
"ORANGE",
"RED ORANGE",
0 // or NULL
};
will never be met because there are no elements that are equal to \0
.
永远不会遇到,因为没有元素等于\0
。
All the arrays are placed exactly one after another in the memory. Your i
keeps increasing forever. On i = 1
you are on the first string in oranges
. On i = 2
you are on the second element.
所有的数组都在内存中一个接一个地放置。你的i
永远增加。Oni = 1
你在第一个字符串上oranges
。在i = 2
你的第二个元素上。
After that, i
becomes 3. Since right after oranges
, in your mamory lies the apples
array, your pointer starts pointing to it and the app prints APPLE
. On i = 4
the pointer is on the lemons
array and the app prints LEMONS
. After that you effectively go out of your own memory which for me results in a crash.
之后,i
变为 3。因为在 之后oranges
,您的内存中存在apples
数组,您的指针开始指向它并且应用程序打印APPLE
. 上i = 4
的指针是在lemons
阵列和所述应用程序的打印LEMONS
。在那之后,你有效地走出了自己的记忆,这对我来说会导致崩溃。
To fix that you need to explicitly add an empty element into each of the arrays, e.g.
要解决这个问题,您需要在每个数组中显式添加一个空元素,例如
const char* oranges[] = {
"ORANGE",
"RED ORANGE",
""
};
回答by Andrea Bergia
You are checking that fruit[i] != '\0'
. That is wrong because fruit[i]
is a char *
, not a char. Furthermore, your vectors aren't terminated. You probably wanted to check whether fruit[i] != 0
, or *fruit[i] != '\0'
. In the first case, you need to terminate the vectors like this:
你正在检查那个fruit[i] != '\0'
。这是错误的,因为fruit[i]
是char *
,而不是字符。此外,您的向量不会终止。您可能想检查是否fruit[i] != 0
, 或*fruit[i] != '\0'
。在第一种情况下,您需要像这样终止向量:
void printFruit(const char** fruit, int fruitSize){
int i =0;
while (i < fruitSize){
std::cout << "---------------------\n";
std::cout << fruit[i] << "\n";
i++;
}
}
int main (int argc, const char * argv[])
{
// The second parameter can be worked out by the compiler.
printFruit(oranges, sizeof(oranges)/sizeof(const char*) );
return 0;
}
In the second:
在第二:
const char* oranges[] = {
"ORANGE",
"RED ORANGE"
};
const char* apples[] = {
"APPLE"
};
const char* lemons[] = {
"LEMON"
};
回答by Component 10
IMHO, you'd be better off knowing exactly how many elements that you're dealing with. The bad news is that a simple array of character pointers won't tell you (it's not a std::vector
) so you won't be able to discover it in your printFruit
function.
恕我直言,你最好确切地知道你正在处理多少元素。坏消息是一个简单的字符指针数组不会告诉您(它不是std::vector
),因此您将无法在您的printFruit
函数中发现它。
The good news, however, is that it is available at compile time so you don't have to worry about the overhead of finding it out. The following shows what I mean:
然而,好消息是它在编译时可用,因此您不必担心查找它的开销。下面显示了我的意思:
while (fruit[i] != ' const char* oranges[] = {
"ORANGE",
"RED ORANGE",
0
};
const char* apples[] = {
"APPLE",
0
};
const char* lemons[] = {
"LEMON"
,0
};
'){
std::cout << "---------------------\n";
std::cout << fruit[i] << "\n";
i++;
}
Since you're using C++ though, I'd strongly recommend that you use one of the standard collection types such as vector
as they're much safer when it comes to bounds checking, memory allocation etc.
由于您使用的是 C++,我强烈建议您使用其中一种标准集合类型,vector
因为它们在边界检查、内存分配等方面更安全。
回答by Adrian Herea
const char* oranges[] = {
"ORANGE",
"RED ORANGE",
"##代码##"
};
in memory will looks somthing like
在内存中看起来像
"ORANGE""RED ORANGE""APPLE""LEMON"
“橙”“红橙”“苹果”“柠檬”
##代码##will end when you reach the end of "big array" which is "LEMON"
当你到达“大阵列”的末尾时将结束,即“LEMON”
to make your code working you need memory to looks like "ORANGE""RED ORANGE"0"APPLE"0"LEMON"0 so
为了让你的代码工作,你需要内存看起来像 "ORANGE""RED ORANGE"0"APPLE"0"LEMON"0 所以
##代码##回答by Andrey Chernukha
Your arrays are located in memory one after another so it continues printing others because the while
condition is false
您的数组一个接一个地位于内存中,因此它会继续打印其他数组,因为while
条件是false