|
-
Jan 17th, 2003, 06:34 PM
#1
Thread Starter
Frenzied Member
Pointer arrays
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;
}
Last edited by aewarnick; Jan 17th, 2003 at 06:38 PM.
-
Jan 17th, 2003, 10:24 PM
#2
Addicted Member
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....
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;
}
Regards,
ChuckB
-
Jan 17th, 2003, 10:42 PM
#3
Hyperactive Member
Pointer array is an array to store pointers.
-
Jan 18th, 2003, 10:03 AM
#4
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];
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 18th, 2003, 02:17 PM
#5
Thread Starter
Frenzied Member
Ok, thank you very much! All of you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|