Thread: Stack around the variable was corrupted

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    5

    Stack around the variable was corrupted

    Hello everyone!

    I am struggling with executing my C program, so when I run my program the following error appears after this part of code

    Code:
    void getName(struct Name* name) {
    
        char yesNo;
    
        printf("Please enter the contact's first name: ");
        scanf("%30[^\n]%*c", name->firstName);
    
        printf("Do you want to enter a middle initial(s)? (y or n): ");
        scanf("%s%*c", &yesNo);
    
        if (yesNo == 'y' || yesNo == 'Y') {
            printf("Please enter the contact's middle initial(s): ");
            scanf("%6[^\n]%*c", name->middleInitial);
        }
    
        printf("Please enter the contact's last name: ");
        scanf("%35[^\n]%*c", name->lastName);
    }
    And the error message is "Stack around the variable "yesNo"was corrupted". How can I fix that?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,286
    > scanf("%s%*c", &yesNo);
    The %s conversion needs at least TWO characters of storage
    - the char you type
    - the \0 character.

    Perhaps with a leading space to %c, you can get what you want.
    scanf(" %c%*c", &yesNo);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    5
    Yes, this one worked. Thank you a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Corrupted variable stack?
    By The Geeky Coder in forum C Programming
    Replies: 4
    Last Post: 01-08-2014, 12:56 AM
  2. Stack around the variable 'Output' was corrupted
    By jacksb68 in forum C Programming
    Replies: 5
    Last Post: 09-08-2009, 08:55 AM
  3. Stack around the variable 'total' is corrupted
    By diyteam in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2008, 11:45 PM
  4. stack around the variable corrupted
    By chintugavali in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2008, 01:01 PM
  5. Stack around variable corrupted?
    By timmygee in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2005, 08:19 AM

Tags for this Thread