数组char * [英] Array of char *

查看:275
本文介绍了数组char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数组指针的问题。我已经浏览过谷歌,我的尝试是徒劳的到目前为止。

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

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

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.

尝试创建一个指针。

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?

推荐答案

为什么不使用字符串和字符串的Vector来存储名称?
smpl:

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;
}

这篇关于数组char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆