`Build Error` Error -1073741819 - C

2019-08-14 05:08发布

问题:

my assignment is to take in a data file of all the possible letters for each die from the game Boggle. Here's a copy of the data file:

   D R L X E I
   C P O H S A
   N H N L Z R
   W T O O T A
   I O S S E T 
   N W E G H E
   B O O J A B
   U I E N E S
   P S A F K F
   I U N H M Qu
   Y R D V E L
   V E H W H R
   I O T M U C
   T Y E L T R
   S T I T Y D
   A G A E E N

Each die takes 6 of the letters, and these are stored in a linked list. When I have tried to run the following code, I keep getting an error code: C:\Dev-Cpp\Makefile.win [Build Error] [Untitled1.o] Error -1073741819

I've tried using 3 different IDEs, but always seem to get some compiling issue. I can't seem to figure out where I'm going wrong! This isn't complete yet by any means but would rather figure this out before I keep going deeper and deeper. Thanks in advance!

code:

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


#define LENGTH 80

struct boggleDataNode{
       char data[3];
       struct boggleDataNode *nextData;
}*head;

struct boggleDieSideNode{
       char dieSideData[3];
       struct boggleDieSideNode *nextSide;
}*head1;

void readData(struct boggleDataNode temp);

int main(){
    int counter = 0;
    struct boggleDataNode *head;
    struct boggleDieSideNode *head1;
    struct boggleDataNode *temp;
    *head = NULL;
    *head1 = NULL;
    *temp = *head;
    readData(*temp);


    system("pause");
    return 0;
}
void readData(struct boggleDataNode temp){
    //initializing variables including
    //opening the input file
    FILE *input = fopen("BoggleData.txt","r");
    int name =0; 
    int id=0;
    char data[96] ={};

    //error checking that the file opened
    if (input == NULL){
        fprintf(stderr, "Can't open input file!\n");
        exit(1);
    }
    while( fscanf(input, "%s", &data) != EOF)
           printf("%s", data);

}

回答1:

In your code, change

struct boggleDataNode{
       char data[3];
       struct boggleDataNode *nextData;
}*head;

to

struct boggleDataNode{
       char data[3];
       struct boggleDataNode *nextData;
};

as it seems you don't need a global pointer for that structure anyway. As per your usage, it will be shadowed by the local head.

Same goes for head1 also.

Next, you assign NULL to a pointer itself, not to the variable it points to. (FWIW, NULL itself is a pointer, an invalid one, though).

Change

*head = NULL;

to

head = NULL;


回答2:

Whatever else in your program logic is wrong, there's also a syntax error in

 char data[96] ={};

since ISO C forbids empty array initializers. If your build breaks due to this, it may explain the "Build Error". If you want data to be zero initialized, a single = { 0 } will do.

It is also a good idea to crank up the warning level of your compiler by adding appropriate options. What system (OS) is this? Which compiler?



回答3:

try removing the ampersand from "&data" because i believe you're trying to read a string, so pass the string, not the direccion.

 while( fscanf(input, "%s", &data) != EOF)
       printf("%s", data);

so you'd have this:

  while( fscanf(input, "%s", data) != EOF)
       printf("%s", data);


回答4:

strongly suggest, when compiling to enable all warnings, then fix those warnings. Afterall, the compiler knows the language much better than us humans.

The following code

1) compiles cleanly,

2) has unneeded items either commented out or #if 0...#endif out

3) checks for input errors

4) uses 'perror()' to properly output system function error info including the message related to the current 'errno' value

5) closes the input file after all reading completed

6) the fscanf() will stop after reading one numeric char from the input file because %s will stop when any 'white space' is encountered.

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


//#define LENGTH 80
// Note: surround numeric #defines with parens to avoid text substituion errors
#define MAX_DATA_LEN (96)

#if 0
struct boggleDataNode
{
       char data[3];
       struct boggleDataNode *nextData;
};
#endif

#if 0
struct boggleDieSideNode
{
       char dieSideData[3];
       struct boggleDieSideNode *nextSide;
};
#endif


void readData( void );

int main( void )
{
    //int counter = 0;
    //struct boggleDataNode *head = NULL;
    //struct boggleDieSideNode *head1 = NULL;
    //struct boggleDataNode *temp = NULL;

    readData();


    system("pause");
    return 0;
} // end function: main


void readData()
{
    //initializing variables including
    //opening the input file
    //int name =0;
    //int id=0;
    char data[ MAX_DATA_LEN ];

    FILE *input = fopen("BoggleData.txt","r");
    //error checking that the file opened
    if (input == NULL)
    {
        perror("Can't open input file!\n");
        exit(1);
    }

    // implied else, fopen successful

    while( 1 == fscanf(input, " %s", data)) // note: format string leading space to skip 'white space'
           printf("%s", data);

    fclose( input );
} // end function: readData