storing names in an array

THE PROBLEM HERE IS THAT I'M STORING STRINGS IN ARRAY. (e.g. name[4] - names with four name values such as maria, sarah, karen, dianne).

My program says no errors and runs succesfully but its output is wrong and it terminated incorrectly like closing the editor.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

void main(void)
{
clrscr();
char *name[4];

for(int i=0; i<4; i++){
cout<<"\nname: ";
scanf("%s", name[i]);
}
cout<<"names entered\n\n";
for(int j=0; j<4; j++){
cout<<name[j];
cout<<endl;
}
getch();
}

output:

name: maria

name: sarah

name: karen

name: dianne


names entered

maria
sarah

dianne

thats my code.. its runs but when displaying the names i entered its empty at third display. then exist the editor.

what is the problem?
Where are you allocating memory for the following pointer?
char *name[4];

Also you should never use scanf() to retrieve a C-string without specifying the size of your array.

scanf("%4s", name); // only allow 4 characters.
Topic archived. No new replies allowed.