Search code examples
cbus-error

Bus error in simple C program


I have the following simple program. And I am running "program file.txt" from the command line and getting "Bus error" with no output. I don't know what I'm doing wrong here.

#include<stdio.h>

int main(char *arg[])
{

    FILE *file = fopen(arg[0], "r");

    if (file == NULL) {
        printf("Cannot open file.\n");
        exit(1);
    }

    return 1;
}

Solution

  • could you print out the value of arg[0], suppose the type of main is

    int main(int argc, char* argv[])
    

    and the argv[0] is the name of process, the argv[1] is first argument.