0

EDIT: This is the error I am getting. I am getting this error after the program works perfectly, and I don't know why. I am trying to add in strings into a temp variable which then adds them to a struct array, the name temp value space is 15 and then checks if the name is over 15 characters and asks the user to re enter the string if true. It may be because of the buffer in entering to name var but I don't know.

typedef struct {
char name[15];
int score;
int riskF;
} player_info;

int main()
{
    player_info players[20];
    char name[15];
    int gameN = 0;
    int riskF = 0;
    int accScore = 0;
    int totalplayers = 0;
    int count = 1;
    int length = 0;
    int maxName = 15;

    printf_s("Enter player %d: ", count);
    scanf_s("%s", name, 999);

    length = strlen(name);
    if (length < maxName)
    {
        strcpy_s(players[totalplayers].name, name);
        totalplayers++;
        count++;
    }
    else
    {
        printf_s("\nName too big; please enter a name within 15 characters!\n\n");
    }
    length = 0;



    printf_s("done!");
    return 0;
}

1 Answers1

0

At least two problems are apparent. The first should prevent a successful compile, the next is worse.

1) There are too few arguments to strcpy_s, where its prototype is shown to be:

errno_t strcpy_s(char *strDest, size_t numElements, const char *strSource);

Your statement...

strcpy_s(players[totalplayers].name, name);

...appears to be missing the middle argument, size_t numberOfElements, Because this is not an optional argument in strcpy_s, the code you show should not have compiled.

2) Your code is invoking undefined behaviour. You have configured your scanf_s function to read in more than what your variable name can hold. Beyond the point in your code this occurs, there is no telling how your code will behave. It may work one time, or a hundred times, then not work the next.

char name[15];
....
int maxName = 15;


scanf_s("%s", name, 999);//name can contain only 14 characters and a NULL.
                    ^^^  //you are allowing scanf_s to read more than that.

Set the 3rd argument of scanf_s to match size-1 of the char array you are writing to:

int maxName = 14;//and for same reason, change maxName 
scanf_s("%s", name, 14);//room for NULL
/// or a width specifier can be used
scanf_s("%14s", name, 15);
/// BEST of all, use a portable version: scanf with width specifier
scanf(("%14s", name); //provided similar protection.
                      //note: scanf has not be deprecated by anyone
                      //except Microsoft

Either way, if scanf_s is to protect against buffer overflow, you must set the arguments correctly.

Regarding your comment: I made the 999 buffer 15 before but if there is an input over 15 characters it breaks my code.

You need to allow room for NULL terminator. ( see this C string definition )
If you fill your buffer, sized to contain only 15 char with 15 char of input, the array does not have room for the NULL terminator. The char is array is therefore not a C string, and will not be guaranteed to behave as a string. (UB).

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Yeah I tried to make the `scanf_s` buffer the same but any values entered over the buffer ruin the code. –  May 17 '17 at 13:11
  • @Robert - Yes, I saw your comment above also. I think you are still missing the point about allowing room for NULL termination. Without a NULL character appended to the end of a `char` array, all you have is an array of `char`, not a string. See my edits in post above. – ryyker May 17 '17 at 13:28
  • Yeah, I posted that comment because there wasn't all that edit part sorry. I only realized that the string is set to NULL when I printed the length variable, so I fixed the code by checking for NULL, thanks for the help though –  May 17 '17 at 13:37