MrStrategschi
MrStrategschi

Reputation: 31

C: copy string into list of strings

So I have a list of names and corresponding phone numbers, and I want the user to be able to continuously enter a new name-number pair into that list. The part of my code where I try to do that looks something like this:

char name[20], list_names[1000][20], phone[20], list_phone[1000][20];
int n;

n = 0;
do
{
   printf("Enter name: ");
   scanf("%20[^\n]", name);

   printf("Enter phone number of %s: ", name);
   scanf("%20[^\n]", phone);

   strcpy(list_names[n], name);
   strcpy(list_phone[n], phone);

   n += 1;
}
while (n < 1000);

This usually gives me an error like "incompatible pointer type". I have to do it the indirect way and first store the name in a separate variable. But how do I get the string from that variable into the list? Probably there's something I don't get in the strcpy() part. Thanks for helping out!

Upvotes: 0

Views: 497

Answers (1)

hanie
hanie

Reputation: 1885

try this

        printf("Enter name: ");
        scanf(" %19[^\n]", name);//add one space and turn 20 to 19 (leave space for '\0')

        printf("Enter phone number of %s: ", name);
        scanf(" %19[^\n]", phone);

Upvotes: 1

Related Questions