c++ - How to use pointers with strings? -
i know question not specific let me explain code
char name[5][30]; (int = 0; < 5; i++) cin >> name[i]; (int = 0; < 5; i++) cout<<name[i];
in example above created array of characters can input 5 words each 30 bit length. , works fine when try use pointer when don't know how many words input. error in line 5 saying value of type int cant asigned char , understand error how how pass problem?
int n; cout << "number of names" << endl; cin >> n; int *name; name = new char[n][30]; (int = 0; < 5; i++){ cin >> *name; name++; } (int = 0; < 5; i++){ cout << *name; name++; }
- use
char
, notint
. - incrementing
name
doesn't seem idea because have returned first element before printing. used array indexing operator. - i guess
n
input & output should done instead of fixed5
input & output.
int n; cout << "number of names" << endl; cin >> n; char (*name)[30]; name = new char[n][30]; (int = 0; < n; i++){ cin >> name[i]; } (int = 0; < n; i++){ cout << name[i]; } delete[] name;
Comments
Post a Comment