Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

C: Why do I get the error code "*** stack smashi´ng detected **: terminated" and how do I solve it?

+3 votes
asked Nov 5 by Emil Nørspang Christensen (150 points)

This is my code (not yet finished), written in C.

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

    //defining struct for customer management
struct customers {
    char regNumber;     //registration number of the car
    char carBrand;      //brand og the car
    char rimType;       //rim type, aluminum or steel
    char tireType;      //tire type, summer or winter
    int queuePlace;     //place in queue
    int waitTime;       //time till customer is treated
};

    //functions used
void standardStartQueue (char regNumber[12], char carBrand[12], char rimType[9], char tireType[4]);

int main ()
{
    //defining vairables
    
    char regNumber[12], carBrand[12], rimType[9], tireType[4];
    
    //function that imports standard start queue
    standardStartQueue (&regNumber[12], &carBrand[12], &rimType[9], &tireType[4]);
    
    printf("%s %s %s %s", regNumber, carBrand, rimType, tireType);
}


    //function that imports standard start queue
void standardStartQueue (char regNumber[12], char carBrand[12], char rimType[9], char tireType[4])
{
    FILE *fp;
    
    fp = fopen("queue.txt", "r");
    
    fscanf(fp, "%s%s%s%s", regNumber, carBrand, rimType, tireType);
}   

When i run the compiler i get the error code: 

*** stack smashi´ng detected **: terminated

Can someone help me solve this?

Thank you 3000!

1 Answer

0 votes
answered Nov 9 by Peter Minarik (63,320 points)
standardStartQueue (&regNumber[12], &carBrand[12], &rimType[9], &tireType[4]);

This is wrong. You're supposed to pass a pointer to your buffers, but instead, you pass a pointer to the memory addresses AFTER your buffers.

Correctly it should be

standardStartQueue (&regNumber, &carBrand, &rimType, &tireType);

Disclaimer: I came to this conclusion by looking at your code. You may have further issues, I didn't have the time to run your code.

Good luck! :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...