Can someone please let me know why one can't take an input in arrays of pointer to strings using input stream as in the following code:
char *names[5];
for(int i=0; i<5; i++)
{
cout<<"enter name "<<i+1;
cin>>names[i];
}
The variable "names" is an undefined array of char pointers. That's your first problem. The pointers are undefined and have no memory allocated for them. In your original code example there are five char pointers with undefined values. This is why the program would crash, because it's trying to access memory of the invalid address in the pointers. Another problem is that there is no memory allocated to hold the array of chars coming in from stdin.
You could do something like this to get your original example working without crash:
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char** argv)
{
char names[5][128];
memset(names, 0, sizeof(names));
for (int i = 0 ; i < 5 ; i++ )
{
cout << "enter name " << i+1 << ": ";
cin >> names[i];
}
for (int i = 0 ; i < 5 ; i++ )
{
cout << names[i] << "\n";
}
}
This allocates an array of 5 128 character strings. It also clears the arrays with the memset() as well.
Since this is C++ it would seem to make more sense to do it C++ style.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int NUM_NAMES = 5;
int main(int argc, char** argv)
{
vector<string> names;
names.resize(NUM_NAMES);
for (int i = 0 ; i < NUM_NAMES ; i++ )
{
cout << "enter name " << i+1 << ": ";
cin >> names[i];
}
for (int i = 0 ; i < NUM_NAMES ; i++ )
{
cout << names[i] << "\n";
}
}
cin >> names[i];. >> has no clue how bit the array is and will gleefully write off the end of the array if given enough input. getline or get may be safer choices and have the added advantage of handling names like "Von Doom".Disclaimer:
using char* for reading from std::cin is generally a bad idea and I suggest you learn how to use std::string in future.
Also this question would have been better suited for codereview.SE but I'll ignore that for now.
Here is an example of doing what you want to do, but I strongly insist you avoid doing it in any real code.
char * names[5];
// Allocate names
for (int i = 0; i < 5; i++)
{
// Use 256 characters as a buffer
// any names longer than that might cause errors
names[i] = new char[256];
}
// Do IO
for (int i = 0; i<5; i++)
{
// Append std::endl
std::cout << "enter name " << i + 1 << std::endl;
// This isn't very robust since the user can give anything as a name,
// and they can't use spaces because of how cin works in this example.
std::cin >> names[i];
}
// Use names
for (int i = 0; i < 5; i++)
{
// Printing names is just an example
// you could write the names to somewhere more important
// you could copy them to a smaller location to use less memory
std::cout << names[i] << std::endl;
}
// Dispose of names when done
for (int i = 0; i < 5; i++)
{
// Check pointer isn't null
if (names[i] != nullptr)
{
// Never forget to delete
delete names[i];
}
}
ptr? How, if at all, is it related tonames?cin>>names[i]expectsnames[i]to be a valid pointer, pointing to a sufficiently large array of characters where the input can be stored. But in your example,names[i]is an uninitialized pointer containing random garbage. The example exhibits undefined behavior, by way of accessing uninitialized object.char *? You might forget to allocate the buffer, forget todeletethe buffer or the buffer might not be long enough. Isstd::stringnot an option?