Why can't I input into the pointer array?
#include <iostream>
#include <cstdlib>
#include <cctype>
int main(){
char *g[5];
for (int i=0; i<5; i++)
cin>> g[i];
for (int i=0; i<5; i++)
cout<< g[i]<< endl;
system("PAUSE");
return 0;
}
Printable View
Why can't I input into the pointer array?
#include <iostream>
#include <cstdlib>
#include <cctype>
int main(){
char *g[5];
for (int i=0; i<5; i++)
cin>> g[i];
for (int i=0; i<5; i++)
cout<< g[i]<< endl;
system("PAUSE");
return 0;
}
Hi,
Pointers and arrays always mess with me. Here is how I modified your program to work. I would give an explanation but I would probably describe it incorrectly. Some of the gurus on this site will do a much better job than I. :-)
Anyhow, this code works....
Regards,Code:#include <iostream>
#include <cstdlib>
#include <cctype>
int main(){
char g[5];
char *p = g; //this creates a pointer to g...I think
for (int i=0; i<5; i++)
cin >> p[i]; //this loads input into address
for (int i=0; i<5; i++)
cout<< g[i]<< endl;
system("PAUSE");
return 0;
}
ChuckB
Pointer array is an array to store pointers.
Yep, you're unlikely to receive a pointer as input from the user. And if that should be an array of strings: you don't allocate memory from them.
For an array of strings you'd better write:
Code:#include <string>
using namespace std;
//...
string ar[5];
Ok, thank you very much! All of you.