cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Array of pointers to char
Array of pointers to char
Nov 9, 2009 at 3:58am UTC
need2no
(2)
Sorry posted in wrong section b4
Ok Im just tryna to figure this out,
If I type a code
char *name[3]={"DJ","Duane","Dale"};
for(int i=0;i<3;i++)
{
cout<<name[i]<<"\n";
}
the names output perfectly fine
but if i try to input dynamically(something simple)...
char *name[1];
cin>>name[0];
the program crashes as soon as i press enter, why is this?
If there a special format i have to use for the input?
Nov 9, 2009 at 5:04am UTC
writetonsharma
(1461)
in the first case it is auto initialized.
in the second one you need to allocate memory first and only then you can use.
you have 3 pointer, allocate for each pointer and then use.
like this way:
1
2
3
*(name + 0) =
new
char
[100]; cin.getline(name[0],100); std::cout << name[0] << std::endl;
infact when you dont allocate memory the compiler will warn you and which you should have noticed.
Nov 9, 2009 at 5:31am UTC
need2no
(2)
My crappy compiler didn't give me any warning but that u soo much.
Topic archived. No new replies allowed.