C++ 字符数组 *

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2930563/
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-08-28 11:31:37  来源:igfitidea点击:

Array of char *

c++

提问by user353060

I am having problems with array pointers. I've looked through Google and my attempts are futile so far.

我在使用数组指针时遇到问题。我已经浏览过谷歌,到目前为止我的尝试都是徒劳的。

What I would like to do is, I have a char name[256]. I will be 10 of those. Hence, I would need to keep track of each of them by pointers.

我想做的是,我有一个字符名称[256]。我将是其中的 10 个。因此,我需要通过指针跟踪它们中的每一个。

Trying to create a pointer to them.

试图创建一个指向它们的指针。

int main()
{
    char superman[256] = "superman";
    char batman[256] = "batman";
    char catman[256] = "catman";
    char *names[10];
    names[0] = superman;
    names[1] = batman;
    system("pause");
    return 0;
}

How do I actually traverse an array of pointers?

我如何实际遍历指针数组?

回答by nos

names[0] is a char* to whatever you stored in names[0] (which in this case is a pointer to the first element in your supermanarray) thus your guess at e.g cout << names[0] << endl;is correct.

names[0] 是您存储在 names[0] 中的任何内容的 char*(在这种情况下,它是指向superman数组中第一个元素的指针),因此您对 eg 的猜测cout << names[0] << endl;是正确的。

If you want to traverse that array, you need to know when to stop so you're not traversing pointers you havn't yet initialized- if you knowyou have initialized 2 of those pointers, you could do e.g.

如果你想遍历那个数组,你需要知道什么时候停止,这样你就不会遍历你还没有初始化的指针——如果你知道你已经初始化了其中的 2 个指针,你可以这样做

for(int i = 0; i < 2 ; i++) {
  std::cout << names[i] << std::endl;
}

As an alternative, place a NULL pointer after the last element you have initialized(make sure there's room for that NULL pointer) e.g.

作为替代方案,在您初始化的最后一个元素之后放置一个 NULL 指针(确保该 NULL 指针有空间)例如

names[2] = NULL;
for(int i = 0; names[i] != NULL ; i++) {
  std::cout << names[i] << std::endl;
}

回答by OlimilOops

why not use strings and a Vector of strings to store the names? smpl:

为什么不使用字符串和字符串向量来存储名称?标准:

#include <string>
#include <iostream>
#include <Vector>

//using namespace std;

int main(void) {
    std::string superman = "superman";
    std::string batman = "batman";
    std::vector<std::string> names;
    names.push_back(superman);
    names.push_back(batman);
    for (unsigned int i = 0; i < names.size(); ++i) {
        std::cout << names[i] << std::endl;
    }
    char c; std::cin >> c;
}

回答by smcameron

char *names[] = { "superman", "batman", "whatever", NULL };

...

...

for (int i = 0; names[i] != NULL; i++)
    printf("%s\n", names[i]);

He might not want to use a vector because he might be using C, not C++.

他可能不想使用向量,因为他可能使用的是 C,而不是 C++。

edit: I see he tagged it C++ though.

编辑:我看到他把它标记为 C++。

回答by Mike

Using arbitrary fixed length arrays to manipulate strings is a complete no no. In my company, this code would be illegal, period. This practice is exactly the cause of most security breaches and it's what makes C/C++ (that uses this type of code) notoriously unsecure. I highly recommend the C++ solution from "Oops".

使用任意固定长度的数组来操作字符串是完全不可以的。在我的公司,这段代码是非法的,句号。这种做法正是大多数安全漏洞的原因,也是导致 C/C++(使用这种类型的代码)非常不安全的原因。我强烈推荐来自“Oops”的 C++ 解决方案。

回答by Thomas Matthews

First try using std::string, this will relieve you of memory allocation and deallocation issues.
Second, use std::vector<string>which dynamically expands as needed.

首先尝试使用std::string,这将减轻您的内存分配和释放问题。
其次,std::vector<string>根据需要使用which 动态扩展。

If you mustuse char *, you will need an array of pointers to char *.
This is declared as:

如果必须使用char *,则需要一个指向 的指针数组char *
这被声明为:

char * array_of_C_strings[10]; // Define an array of 10 pointers to char *.

char * array_of_C_strings[10]; // 定义一个包含 10 个指向 char * 的指针的数组。

If the strings are fixed length:

如果字符串是固定长度的:

char array_of_fixed_length_C_strings[10][256]; // Array of 10 C-Strings that are max. size 256.

char array_of_fixed_length_C_strings[10][256]; // 最多 10 个 C 字符串的数组。尺寸 256。

Assignment:

任务:

char hello[32];
strcpy(hello, "Hello");
array_of_C_Strings[0] = hello;  // Note: only pointers are copied
strcpy(array_of_fixed_length_C_Strings[2], hello);  // Copy actual content of string.

With std::stringand std::vector<std::string>:

随着std::stringstd::vector<std::string>

std::string hello = "hello";
std::vector<std::string> string_container;
string_container.push_back(hello);
string_container.push_back("world!");
std::cout << string_container[0]
          << ' '
          << string_container[1]
          << "\n";

The example using std::stringand std::vectorlooks simpler than an array of char *, but that is my opinion, YMMV.

使用std::string和的示例std::vector看起来比 的数组简单char *,但这是我的观点,YMMV。