C语言编译错误求助:语法及fread参数不足等问题排查
Hey there! Making the jump from Pascal to C comes with some syntax adjustments—let's walk through each of your errors and break down how to fix them, since these are super common for folks coming from Pascal's more verbose (but different) type system.
Let's Tackle Each Error One by One
1. Error: expected expression before struct
This usually stems from a key difference between Pascal and C's struct handling:
- In Pascal, once you define a
Booktype, you can use it directly. In C, if you define a struct like:
You need to either prefix variable declarations withstruct Book { char title[50]; int book_id; };struct(e.g.,struct Book my_book;) or usetypedefto create a shorthand for the struct type:typedef struct { char title[50]; int book_id; } Book; // Now you can declare variables like this (no struct prefix needed) Book my_book; - Another possible cause: You tried to use
structin an expression where it doesn't belong, likestruct.title(this is invalid—you need to use a struct variable, not the type itself).
2. [Error] expected identifier or '(' before '.' token
This error pops up when you use the . member access operator on something that isn't a struct variable. For example:
- If you wrote
struct Book.title(using the struct type instead of a variable), that's wrong. You need to access members on an instance of the struct:Book my_book; strcpy(my_book.title, "The C Programming Language"); // Correct - Double-check your function definitions too—this error can also happen if you have a typo like
void .display_book()instead ofvoid display_book().
3. [Error] expected expression before 'books'
Assuming books is a struct array or variable, this usually means one of two things:
- You're using
booksas a type instead of a variable. For example, if you declaredstruct Book books[];but then tried to use it in an expression likestruct Book books = ...(redeclaring it incorrectly). - You forgot to declare
booksbefore using it. In C, variables must be declared before they're referenced (unlike some Pascal dialects where you can declare them later in the block).
4. [Error] too few arguments to function 'fread'
C's fread requires 4 mandatory arguments (Pascal's file-reading functions have different signatures):
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
Let's break down what each means:
ptr: Pointer to the buffer where you'll store the read data (e.g., yourbooksarray)size: Size of a single element (e.g.,sizeof(Book))nmemb: Number of elements you want to readstream: Pointer to the open file (fromfopen())
Example of correct usage:
Book books[10]; FILE *file_ptr = fopen("library.dat", "rb"); if (file_ptr != NULL) { // Read 10 Book structs into the books array fread(books, sizeof(Book), 10, file_ptr); fclose(file_ptr); }
You probably missed one or more of these arguments (like omitting the file pointer or number of elements).
Quick Pascal-to-C Tips to Avoid These Issues
- Structs: Always remember to either use
structprefixes ortypedeffor shorthand. - Variable Scope: Declare variables at the top of blocks (or before use) until you're comfortable with C99's block-scoped declarations.
- Function Calls: Double-check standard library function signatures (like
fread)—C is stricter about argument counts and types than Pascal. - Passing Structs: In Pascal,
varparameters pass by reference; in C, you'll need to use pointers if you want to modify a struct in a function (e.g.,void update_book(Book *book_ptr)instead ofvoid update_book(Book book)).
内容的提问来源于stack exchange,提问作者Ingrid Ryes

