Change
for(i=1;i<6;i++)
{
gets(input);
strcpy(people[1], input);
input[260] = '\0';
}
to
for(i=0;i<5;i++)
{
gets(input);
strcpy(people[i], input);
input[0] = '\0';
}
Now to be clear i changed the loop from 0 to 5 instead of 1 to 6 because
array indices start from 0.
In the strcpy function call you passed the same value again and again which is 1, I changed it to the loop variable i, which is the correct way.
In your code snippet you assigned the value of input[260] = '\0'
which is also wrong.
'\0' is used to denote the end of a string
so as you have to empty your character array so
'\0' should be assigned to the first index of the array to denote that
the array is empty.
Now in the second loop, since you have stored 5 names so the loop should be from i=0
to i<5
instead of i<6
So change
for(l=0;l<6;l++)
printf("%s\n", people[l]);
to
for(l=0;l<5;l++)
printf("%s\n", people[l]);
And also you used an extra curly brace after the last printf statement. Remove it and your code is fixed.
Since you have used the return type of the main function as int
int main()
So it would return an integer value, so
you should use a return statement
before the last curly brace like this
return 0;
You have declared a character array named source[]
with a global scope but you haven't used it anywhere in your code so it's better if you remove it.
And also properly
indent your code using white spaces and tabs to make it understandable
and readable
, with code indentation your code will be more readable and you won't miss any curly brace or use extra ones like you used in your code.
To sum up your new code will look like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char people[5][260];
char input[260];
int i, l;
printf("please enter 5 names\n");
for(i=0;i<5;i++)
{
gets(input);
strcpy(people[i], input);
input[0] = '\0';
}
for(l=0;l<5;l++)
printf("%s\n", people[l]);
return 0;
}