C++ 如何声明已在类中声明的 char 变量的 Char 数组?

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

How to declare the Char array of char variables already declared in class?

c++arraysvariablespointerschar

提问by Dhruv Patel

#include <stdio.h>
#include <iostream>

using namespace std;

//char* b[6] = new char[6];

char a[6] = {'b','c','d','e','f','g'};
char c[6] = {'a','b','d','d','f','g'};

int main()
{
    char d[][6]={*a,*c};



    for (int x = 0 ; x < 1; x++)
    {
        for(int y = 0; y<6; y++)
        {
            char test = d[x][y];
            cout << test <<"\n";
        }
    }
    return 0;
}

This code is C++ code. I am trying to create a class where it stores the char array. Then there is another char array of array storing already declared char variables. It compiles fine but it doesn't work out to as it should. It doesn't get me the right value that it should when the program tries to print the value

这段代码是 C++ 代码。我正在尝试创建一个类来存储字符数组。然后还有另一个 char 数组存储已声明的 char 变量。它编译得很好,但它没有像它应该的那样工作。当程序尝试打印值时,它没有给我正确的值

回答by P0W

May be you meant array of pointers:

可能你的意思是指针数组:

char *d[]={a,c};

回答by edwinc

typedef std::vector<char>          VectorChar;
typedef std::vector< VectorChar* > VectorVectorChar;

struct V
{
  V() : _v{ '0', '1', '2' } {}

  VectorChar _v;
};

int main(void)
{
    V arrV[5];

    VectorVectorChar vvc;
    for ( auto& v : arrV )
       vvc.push_back( &v._v );

    // print them
    for ( auto pV : vvc )
    {
      for ( auto c : *pV )
          cout << c << ' ';
      cout << '\n;
    }

    return 0;
}

回答by Ashif

what i understood from the question that, you want to create class to store char array, which already initialized.

我从问题中了解到,您想创建类来存储已经初始化的字符数组。

#include <stdio.h>
#include <iostream>

    char a[6] = {'b','c','d','e','f','g'};  // Initialized character array. MAX 6

    // This class will hold char array
    class Test {    
    public:
        void setName(char *name);
        const char* getName();
    private:
        char m_name[6]; // MAX 6 ( since you already initialized(MAX 6), So no need dynamic memory allocation. )
    };

    void Test::setName(char *name) {
        strcpy(m_name, name); // Copy, already initialized array
    }

    const char* Test::getName() {
        return m_name;
    }

   int main(int argc, char** argv) {
    {
        Test foobar;       
        foobar.setName( a );  // set the pointer which point to starting of the initialized array.
        cout << foobar.getName();
        return 0;
    }

回答by mohagali

char a[6] = {'b','c','d','e','f','##代码##'};
char c[6] = {'a','b','d','d','f','##代码##'};
char* d[]= {a,c};

for (int x = 0 ; x < 2; x++)
{
    for(int y = 0; y < 6; y++)
    {
        char test = d[x][y];
        cout << test << "\n";
    }
}

return 0;