OK, I have a assignment due to use dynamic arrays to make a program that requires user input.It has to be dynamic array, and we never went over vectors in school..as I've seen it used in other not so similar programs, and the result is tabular output like:

Canidates          Votes Received       % of Total Votes
Name                    5000                 25.91
Name                    4000                 20.73
Name                    6000                 31.09
.
.

That said, my trouble problem is the dynamic char array. Not sure why this is not working. (can be in int main(), no need for class..)

 int size;
 char* name = new char[25];

 cout << "Enter num of names: ";
 cin>>size; 
 for(int i=0;i<size;i++){
 cout << "Enter your name: ";
 cin>>name[i];             //cin.getline(name,25); tried this first...not it.
 }
 for(int i=0;i<size;i++){
 cout<<name[i]<<endl;
 }

Wanting size to set how many names will be entered. for() loop being how many times cin will get names. Second for() to cout the names. (Not formatted for tabular obviously) Just trying to figure out why this is not working. I have tried every forum/example I can find. Just can't find an "English" version I can understand. HELP please....(again it's homework and late already : ( Thank you.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

char[25] can only hold '1' string of 25 character length, each character is 1 byte of data (char)

for example, this isn't dynamic or anything, but if you did this:
char[25][15] you could store 25 names, which are up to 15 characters long

name will be able to hold only 1 letter, so I don't think that's what you want.


// allocate memory
  char **name = new char*[num_strings];
  for (int i = 0; i < num_strings; ++i)
    name[i] = new char[15];

// Edited some weird mistakes in my thought process...

You also have a problem with cin, since you want to use an array of char, you'll need to address it like this:
std::cin >> name; // BEFORE you say this is the same as what you are doing, it isn't. It's different because in my example, name is 2D array ( name[25][15] )

In the example you gave, you should have done this:
std::cin >> name; // full stop
But then you would only have 1 name

Good luck!

Edit: Oh, you have to iterate to delete if you do it like this also. Worth mentioning probably.

// de-allocate memory
  for (int i = 0; i < num_strings; ++i)
    delete [] name[i];
  delete [] name; // note that i iterate first in this example, and then delete the base array -after-, reverse of original example

Edited 6 Years Ago by Unimportant: n/a

char[25] can only hold '1' string of 25 character length, each character is 1 byte of data (char)

for example, this isn't dynamic or anything, but if you did this:
char[25][15] you could store 25 names, which are up to 15 characters long

name will be able to hold only 1 letter, so I don't think that's what you want.


// allocate memory
  char **name = new char*[num_strings];
  for (int i = 0; i < num_strings; ++i)
    name[i] = new char[15];

// Edited some weird mistakes in my thought process...

You also have a problem with cin, since you want to use an array of char, you'll need to address it like this:
std::cin >> name; // BEFORE you say this is the same as what you are doing, it isn't. It's different because in my example, name is 2D array ( name[25][15] )

In the example you gave, you should have done this:
std::cin >> name; // full stop
But then you would only have 1 name

Good luck!

Edit: Oh, you have to iterate to delete if you do it like this also. Worth mentioning probably.

// de-allocate memory
  for (int i = 0; i < num_strings; ++i)
    delete [] name[i];
  delete [] name; // note that i iterate first in this example, and then delete the base array -after-, reverse of original example

thanks for the quick response. My stupid C++ book says "char **name = new char" is dynamic array..I'm lost. Haven't been doing C++ long. And zero help/resources. Just looking for anyway to get "Dynamic array" to enter how ever many names need to be entered - user input by number I'm guessing. ((((sigh)))).

char **name;
means you initialize a variable which is a pointer to a pointer to char.

any array is a pointer, the value in the [] operator is an offset.

char array[5]; is a table of 5 characters, array[3] is the address of array[0] + 0x03

char *a; // this is creating a pointer to char
a = new char[5]; // this allocates 5 bytes into heap memory, a is now the address of the first byte (a is effectively &char[0])

char **a; // this means that a points to an array of arrays. remember that an array is a pointer in the first place.

I think the wordiness is confusing.

Basically **name means that *name points to an array, and **name points to an array of *name (forgive the loose use of this variable name please)

Hope that clears up any misunderstanding you might have about that.

As for the keyword new, it just means that you're allocating heap memory.
You could delete the array and make a new one at any time, it is by this process that STL classes such as std::vector automatically resize themselves during run time.

Good luck!

Edit: Oh also, if you're allowed to use STL, it's way easier to do this:

std::vector<std::string> names;
names.push_back( any_string );

It will grow for you, without any additional code required. std::string is also very easy to use.

Edited 6 Years Ago by Unimportant: n/a

char **name;
means you initialize a variable which is a pointer to a pointer to char.

any array is a pointer, the value in the [] operator is an offset.

char array[5]; is a table of 5 characters, array[3] is the address of array[0] + 0x03

char *a; // this is creating a pointer to char
a = new char[5]; // this allocates 5 bytes into heap memory, a is now the address of the first byte (a is effectively &char[0])

char **a; // this means that a points to an array of arrays. remember that an array is a pointer in the first place.

I think the wordiness is confusing.

Basically **name means that *name points to an array, and **name points to an array of *name (forgive the loose use of this variable name please)

Hope that clears up any misunderstanding you might have about that.

As for the keyword new, it just means that you're allocating heap memory.
You could delete the array and make a new one at any time, it is by this process that STL classes such as std::vector automatically resize themselves during run time.

Good luck!

Edit: Oh also, if you're allowed to use STL, it's way easier to do this:

std::vector<std::string> names;
names.push_back( any_string );

It will grow for you, without any additional code required. std::string is also very easy to use.

Much more clear than the text book even begins to try explain. Thank you for your time and effort. I'll have to play with the "wordiness" and the STL std::vector thing. That's all new to me. We only do console apps. and skipped the entire vector chapter. There's a lot to C++...for me anyways. Again, thanks Unimportant..your information is greatly appreciated !

This article has been dead for over six months. Start a new discussion instead.