কিভাবে character array declare করবো যেখানে character variables already declar করা আছে ?


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;
}
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;
    }
char a[6] = {'b','c','d','e','f','\0'};
char c[6] = {'a','b','d','d','f','\0'};
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;

Share on Google Plus

About Engr. Rokon Khan

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment