Variable wrap was damaged

I have a problem with get.

the goal is to get input from the user until he reaches 'Enter'

this is the code:

struct LinkedListNode* executeSection2()
{
    char inputArr [3] = {'\0'};
    struct LinkedListNode* newNode;
    struct  LinkedListNode* head = NULL;

    gets (inputArr);

    while (inputArr[0] != 0) // The user didn't press "Enter"
    {
        newNode=newLinkedListNode();
        newNode->tree=newHuffmanNode(inputArr[0],atoi(inputArr+2));

        head = addNode(&head, newNode);

        gets (inputArr);
    }

    head = buildHuffmanTree(&head);
    return head;
}

it seems ok, the user clicks on 'Enter', the code exits from this point, but after returning I get an error message:

Stack around the variable 'inputArr' was corrupted

I guess I didn’t read the keyboard input correctly. I will be happy for some guidance.

thank.

+4
source share
2 answers

, gets : , , . , , , , undefined.

gets fgets, , - :

fgets (inputArr, 3, stdin);

while, enter , , enter.

fgets '\n' , , enter, '\n':

while (inputArr[0] != '\n') { // The user didn't press "Enter"
    ...
}
+11

gets C . gets - , C . . , . gets - , . man- , . Mac, man- :

, , , , .

, , , . . , . , .

+1

Source: https://habr.com/ru/post/1569213/


All Articles