-1

I basically have a dynamic char array that stores names and have declared it as char **names.

I have a variable called namesnumber which is used to set the size of the names array.

For example,

namesnumbers = 5
names = new char*[namesnumbers];
for (int i=0; i<namesnumbers; i++)
{
   names[i] = new char[65];
   strcpy(names[i], actualName);
}
names = {Mike, Sam, Mark, Bill, George}

This is my return method, however it only returns a memory address and not the actual namess

const char** getNames()
{
   return names;
}

I want my getNames() method to return the 5 names.

Rews Prog
  • 51
  • 1
  • 8
  • 8
    Use std::string and std::vector or std::array. – Ben Nov 14 '14 at 08:37
  • 5
    Why are you using C-style strings and memory allocation in a C++ program ? Use std::string and std::vector and save yourself a lot of grief. – Paul R Nov 14 '14 at 08:38
  • You have the address to the place where the names are stored, you have to dereference it to get the names – Mauricio Trajano Nov 14 '14 at 08:39
  • @MauricioTrajano How would I go about doing that? Sorry, I am new to C++ and I am willing to learn – Rews Prog Nov 14 '14 at 08:41
  • you can dereference it buy index, say if char ** names_returned = getNames(); Then names_returned[0] gives the first name, names_returned[1] gives the second, and so on... Although I only come from a C background and not a C++ one I do suggest you use std::string to avoid complications – Mauricio Trajano Nov 14 '14 at 08:43
  • 3
    If you're new to C++, I suggest you check [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), as well as [a good reference site](http://en.cppreference.com/w/cpp). – Some programmer dude Nov 14 '14 at 08:44
  • 2
    `Sorry, I am new to C++ and I am willing to learn ` As others have pointed out, this is an excellent moment to learn about `std::vector` and `std::string`. Please please please make use of them for your own sanity. – user657267 Nov 14 '14 at 08:44
  • Please help me, I just want to know how pointer arrays work. I will use `std::vector` and `std::string` next time – Rews Prog Nov 14 '14 at 08:45
  • Your code example doesn't compile, please correct this. – Wolf Nov 14 '14 at 08:55
  • Please show more about your actual task! Currently you're talking in a very confuse way about technical terms you don't understand. Help us help you. (-1 so far) – Wolf Nov 14 '14 at 09:02

1 Answers1

2
// namesnumbers = 5
names = new char*[namesnumbers];
for (int i=0; i<namesnumbers; i++)
{
   names[i] = new char[65];
   strcpy(names[i], actualName);
}
// names = {Mike, Sam, Mark, Bill, George}

Equivalent initialization using std library:

  • separate from instantiation:

    std::vector<std::string> names;
    for(const auto& n: actualNames) // actualNames assumed to be an array
        names.push_back(n);
    
  • at instantiation:

    std::vector<std::string> names = { "Mike", "Sam", "Mark", "Bill", "George" };
    
  • returning the five names:

    const std::vector<std::string>& getNames()
    {
        return names;
    }
    
utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • Thanks, what about for not using a vector? Just dereferencing the array? – Rews Prog Nov 14 '14 at 08:48
  • @RewsProg You can't dereference an array. Arrays are not pointers. Pointers are not arrays. – molbdnilo Nov 14 '14 at 08:58
  • What are you seeing in the code snippet you quote at the beginning of your answer? I see a mess that doesn't even compile, regardless of any scope you may try to build around. So, your *Equivalent initialization using* must be an error. (sorry: -1) – Wolf Nov 14 '14 at 09:18
  • @Wolf, it was obvious the point of the question was not if `namesnumbers = 5` is missing a semicolon at the end and is not declared. I didn't bother with that. – utnapistim Nov 14 '14 at 12:35
  • @utnapistim Sure, but why do you quote this nonsense as a reference? – Wolf Nov 14 '14 at 12:40
  • @utnapistim much better now thanks (-1 rem). Maybe a short statement about the wrong syntax would be good. (BTW: this still doesn't compile and the actualName in the loop is still a problem). And, last but no least: great that you took the burden to help in this *very difficult* case :-) – Wolf Nov 14 '14 at 12:56