Array of pointers to char

Nov 9, 2009 at 3:58am
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
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
My crappy compiler didn't give me any warning but that u soo much.
Topic archived. No new replies allowed.