Thread: error: illegal hardware instruction

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    20

    error: illegal hardware instruction

    Hello!

    I am playing around with C and i tried to create a simple program that stores account numbers and names in a struct array.

    To register these parameters i used a int function called registerNewAccount that saves the input from the user. Afterwards i call a struct function (createaccount) to store these parameters. The reason for using a struct function is because i later want to use the function for filehandling (reading and writing to file).

    But when i run the program i get the error:
    "[1] 41323 illegal hardware instruction"

    The error is regarding line 119, which i have highlighted. If i comment that line the program works without any errors.

    I have attached a photo of the two functions (registerNewAccount & createaccount)

    Could someone take a look and perhaps point to where i have made a mistake?

    Here is how my struct looks if thats needed;
    Code:
    struct Bank
    {
        char surName[30];
        char lastName[30];
        char accountNumber[6];
        int balance;
    };
    typedef struct Bank bank;
    
    Attached Images Attached Images error: illegal hardware instruction-ska-rmavbild-2021-03-24-kl-12-53-18-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    It's better if you post code between code tags, instead of fuzzy pictures.
    It's better when you post code between code tags, that you "paste as text", rather than the washed out colour scheme of your IDE.

    What is the size of your bank array?
    What value is numberofAccounts when you call this function.

    A copy/paste of your console showing what you typed is also useful.
    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
    Dec 2017
    Posts
    20
    Quote Originally Posted by Salem View Post
    It's better if you post code between code tags, instead of fuzzy pictures.
    It's better when you post code between code tags, that you "paste as text", rather than the washed out colour scheme of your IDE.

    What is the size of your bank array?
    What value is numberofAccounts when you call this function.

    A copy/paste of your console showing what you typed is also useful.

    Okay thanks. The bank array is set to 100 and numberofaccounts is initialized as 0.

    Here is the entire code so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 1000;
    #define charachter 30;
    
    
    struct Bank
    {
        char surName[30];
        char lastName[30];
        char accountNumber[6];
        int balance;
    };
    typedef struct Bank bank;
    
    
    int registerNewAccount(bank b[], int numberofAccounts);
    void printAllAccounts(bank b[], int numberofAccounts);
    void sortAccounts(bank b[]);
    void changeBalance(bank b);
    int menu(bank b[], int numberofAccounts);
    void saveToFile(bank b[]);
    void readFromFile(bank b);
    bank createaccount(char accountnumber[], char surname[], char lastname[], int balance);
    
    
    
    
    
    
    int main(void)
    {
        int numberofAccounts = 0;
        bank b[100];
        printf("Bank...\n\n");
        numberofAccounts = menu(b, numberofAccounts);
    }
    
    
    int menu(bank b[], int numberofAccounts)
    {
        int val, a[1000] = {0}, size;
        size = sizeof(a) / sizeof(a[0]);
    
    
        for (;;)
        {
            printf(
                "\n"
                "(1) Register new account\n"
                "(2) Show all accounts\n"
                "(9) Exit\n"
                "Ange val:\n");
    
    
            scanf("%d", &val);
            switch (val)
            {
            case 1:
                numberofAccounts = registerNewAccount(b, numberofAccounts);
                break;
            case 2:
                printAllAccounts(b, numberofAccounts);
                printf("Number of accounts = %d\n", numberofAccounts);
                break;
    
    
            default:
                break;
            }
        }
    }
    
    
    int registerNewAccount(bank b[], int numberofAccounts)
    {
        char accountnumber[30], surname[30], lastname[30];
        int balance;
        char quit[] = "q";
        printf("Number of accounts: %d", numberofAccounts);
        for (;;)
        {
            printf("Register new Account\n");
            printf("Assign a new account number (6 digits) (q to quit): ");
            do
            {
                scanf("%s", accountnumber);
                if (strcmp(accountnumber, quit) == 0)
                {
                    printf("You have choosen quit");
                    return 0;
                }
            } while (strlen(accountnumber) < 6);
            printf("Assign a name surname and lastname: ");
            scanf("%s %s", surname, lastname);
            balance = 0;
            b[numberofAccounts] = createaccount(accountnumber, surname, lastname, balance);
            numberofAccounts++;
        }
    
    
        return numberofAccounts;
    }
    bank createaccount(char accountnumber[], char surname[], char lastname[], int balance){
        bank b;
    
    
        strcpy(b.accountNumber, accountnumber);
        strcpy(b.surName, surname);
        strcpy(b.lastName, lastname);
        b.balance = balance;
        
        return b;
    }
    And this is what the console says:

    Bank...




    (1) Register new account
    (2) Show all accounts
    (9) Exit
    Choose:
    1
    Number of accounts: 0
    Register new Account:
    Assign a new account number (6 digits) (q to quit): 123456
    Assign a name surname and lastname: hello world
    [1] 43041 illegal hardware instruction


    But when i comment out the line "b[numberofAccounts] = createaccount(accountnumber, surname, lastname, balance);"
    which is in the function registerNewAccount, everything is fine.
    Last edited by abmoh; 03-24-2021 at 07:46 AM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    On problem is this. You have accountNumber defined as this:
    Code:
    char accountNumber[6];
    This can accept a maximum number of 5 characters, because you need space for the null ('\0') terminator. So when you do this:

    Assign a new account number (6 digits) (q to quit): 123456

    You've written beyond the end of your array and invoked undefined behavior.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    20
    Quote Originally Posted by rags_to_riches View Post
    On problem is this. You have accountNumber defined as this:
    Code:
    char accountNumber[6];
    This can accept a maximum number of 5 characters, because you need space for the null ('\0') terminator. So when you do this:

    Assign a new account number (6 digits) (q to quit): 123456

    You've written beyond the end of your array and invoked undefined behavior.
    Thank you it's working now. I forgot strings needed space for the null character

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal instruction / Bus error
    By Dronor in forum C Programming
    Replies: 4
    Last Post: 06-08-2017, 08:23 AM
  2. Replies: 12
    Last Post: 10-28-2009, 08:51 PM
  3. Illegal Instruction when writting a file
    By guiu in forum C Programming
    Replies: 10
    Last Post: 02-04-2009, 12:18 PM
  4. getting an Illegal Instruction error
    By FJRRulz in forum C Programming
    Replies: 22
    Last Post: 04-27-2008, 09:13 AM
  5. Illegal instruction problem with my program
    By bigtruckguy3500 in forum C Programming
    Replies: 3
    Last Post: 04-16-2007, 11:29 PM

Tags for this Thread