0

so im trying to get input from a user then put the input into a array and then clear the input so it can get more but all i get is these weird symbols here is my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char source[] = "this is the source string";

int main()
{

char people[5][260];
char input[260];

int i, l;

printf("please enter 5 names\n");

for(i=1;i<6;i++)
{
    gets(input);
    strcpy(people[1], input);
    input[260] = '\0';


}

for(l=0;l<6;l++)
    printf("%s\n", people[l]);
}

}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
burn145
  • 5
  • 4
  • 1
    `strcpy(people[1], input);` -> `strcpy(people[i], input);` ? Btw, array indices are zero based in C, your loop should start at `0`. Next, use `fgets` to limit the number of characters read. Also, it should be `input[259] = 0` and it only makes sense to do this before copying. And use `strncpy` instead of `strcpy`. – vgru Aug 27 '17 at 00:25
  • Also , `input[260]` is out of bounds. – Mad Physicist Aug 27 '17 at 00:28
  • Your printf loop is out of bounds too. – Mad Physicist Aug 27 '17 at 00:30

2 Answers2

1

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;
    }
Muktadir Khan
  • 148
  • 3
  • 17
-1
  1. The first for loop should go from 0 to 5, not 1 to 6.

  2. Pretty sure you meant to put i in the brackets here, not 1:

strcpy(people[1], input);

  1. Don't use gets or strcpy; they're insecure. Use fgets and strlcpy or strncpy instead.

  2. input[260] should be input[259]; element 260 is out of bounds.

  3. Set input[259] to zero before you copy the string, not after.

  4. The second loop goes from 0 to 6. Again it should go from 0 to 5, since 5 is the maximum allowed index of an array of size 6.

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
  • ( on comment 5) wouldent that clear input before i copy it to the people[i] array? (on comment 2) that was a miss type i ment to put i in the for loop – burn145 Aug 27 '17 at 00:43
  • so it will print the five strings i put into people – burn145 Aug 27 '17 at 00:44
  • C strings end with a zero terminator. For example, the name "Bob" would take up four characters; the first three for the letters `B`, `o`, and `b`, and the fourth one would contain a zero. This is how C tells how long a string is; when it hits a zero, it knows it's at the end. Setting `input[259]` to zero just makes sure that `input` ends with zero, which it's required to anyway. So this will not clear anything, but it *will* prevent one particular type of buffer overflow error, so you should make sure to do it. – Charles Srstka Aug 27 '17 at 00:47
  • As an aside, this is the same reason you should never use `strcpy`. `strcpy` copies a string of any length, just reading the source string until it hits that zero. So if a user enters a string longer than 260 characters, the extra characters get copied past the end of the array and overwrite random memory elsewhere in the program. This is a great attack vector for hackers; you can stick your own custom code at the end of a string and overwrite the part of memory that contains the program's code, thus hijacking the program into doing God knows what. Never, ever use `strcpy`. – Charles Srstka Aug 27 '17 at 00:49
  • 1
    There's nothing magically unsafe about `strcpy`. Just like everything else, you have to check your buffer lengths. Only `gets` is unsafe. – o11c Aug 27 '17 at 00:58
  • 2
    "Don't use `strcpy`; it's insecure."-- this is a common misperception, [as is the notion that `strncpy()` is supposed to be a safe version of `strcpy()`](https://stackoverflow.com/a/2115015/6879826). `strncpy()` was meant for working with fixed-width strings, not null-terminated C-strings; it can be tricky to use correctly when null-terminated strings are expected. Also, `strlcpy()` is nonstandard. – ad absurdum Aug 27 '17 at 01:06
  • ok thank your for explaining all of this too me. my code is now working as wanted. – burn145 Aug 27 '17 at 01:11
  • `strlcpy()` may be nonstandard, but it's much better than `strcpy` for the machines it's present on, particularly Apple platforms. – Charles Srstka Aug 27 '17 at 22:15