0

C++ newbie.

I have following class and its argument is char*, How do I copy that to member variable char* ? After that I need to know the size of array ?

class TestParam {

public:
  char*[] arr;
  TestParam (const char* Pre, char* Post[]){
    arr = Post;
  }
};
....
TestParam testParam[1] = { TestParam ("aa", (char*[]){"bb","cc","dd"})};

I know about std::string but I had to use char* because I am initializing the object like above in my code. Is it possible by std::string ?

6
  • You won't be able to know the size of any array if all you have is a pointer to the first element. I suggest using user defined types instead of raw pointers and pointers to pointers. Commented Aug 28, 2013 at 7:07
  • 3
    First read about std::string, then read about std::vector. Commented Aug 28, 2013 at 7:07
  • 2
    "char* which is array of strings" - Not accurate. Commented Aug 28, 2013 at 7:07
  • 1
    There are many things wrong here, but the one that stands out is how to you know how many strings you have in your Post array? Until you know the answer to that question you can't begin to address the other problems. As others have said, the easy answer is std::vector and std::string. Trust us, it might seem like yet another new thing to learn about, but it will be much much simpler in the long run. Commented Aug 28, 2013 at 7:11
  • @john the number of strings in Post array are going to be fixed. Commented Aug 28, 2013 at 7:14

4 Answers 4

2

You need to allocate sufficient memory to the destination pointer and then use std::copy.

Tip: Consider using std::string instead.

Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest a solution where you use std::string and std::vector instead, like this:

class TestParam {
public:
    std::vector<std::string> arr;

    TestParam (const std::string& Pre, const std::vector<std::string>& Post){
        arr = Post;
    }
};

...

TestParam testParam[1] = { TestParam ("aa", {"bb","cc","dd"})};

5 Comments

Its giving 'no matching function for call to TestParam::TestParam(const char [3], <brace-enclosed initializer list>)'.... Do I need to typecast it to (std::vector) ?
@VijayC It shouldn't be needed. What compiler (and version of it) are you using?
I am using g++ on ubuntu
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
@VijayC Try adding the -std=c++11 (or if you have an older version -std=c++0x) option when building.
0

You first need the number of elements in Post - that needs to be passed in as another argument, unless Post is defined to have a NULL last element, in which case you need to count the non-NULL elements first.

Once you know that, you can allocate memory for arr.

Finally, you can either perform a shallow copy by assigning the pointer values (eg. arr[i] = Post[i], or a deep copy by duplicating each string. You haven't told us which you need.

Comments

0
#include <iostream>
#include <vector>

class TestParam {
    public:
    template <std::size_t N>
    TestParam(const char* pre, const char* (&arr_ref)[N]) {
        arr.reserve(N);
        for(const char* p: arr_ref) {
            if(p) arr.push_back(std::string(p));
        }
    }
    std::vector<std::string> arr;
};

int main() {
    const char* a[] = { "Hello", " " , "World" };
    TestParam param((char*)0, a);
    for(const std::string& s: param.arr) std::cout << s;
    std::cout << std::endl;
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.