C语言结构体为何接收整数值(字符)而非指针(字符串)?
Hey there! Let's tackle this C struct issue you're hitting when trying to use variables like class or name with your experiment assignment. First, let's recap what your task is all about to make sure we're on the same page:
This is a C programming lab assignment focused on:
- Writing a program to read a specific formatted file
- Storing the course information from the file into a struct
- Practicing passing structs in/out of functions and printing their elements
The target file format looks like this:
CSCI112 Programming with C 3 CSCI127 Joy and Beauty of Data 4 COMX112 Interpersonal Skills in the Workplace 1 HSTA101 American History 4 XXXXXXX XXXXXX X
Common Issues & Fixes for class/name Variables in Structs
1. Using class as a variable/struct member name
Even though class isn't a standard keyword in C, many compilers (like GCC with certain extensions enabled) treat it as a reserved identifier. Plus, if you're coming from C++ experience, it's easy to forget that class is a keyword there but causes conflicts in C compilers.
Fixes:
- Swap it out for a similar, safe name like
course_category,class_code, orcourse_track - If you really need to use the term, add an underscore prefix (e.g.,
_class) — just be careful not to clash with system-reserved identifiers that start with underscores
2. Troubleshooting name variable/struct member issues
If name is giving you grief, it's usually a scope or declaration problem:
- Double-check that your
namevariable (or struct member) is properly declared before you try to pass it - Make sure your struct definition is set up correctly. For example:
// A solid struct definition for your course data typedef struct { char course_id[10]; char course_name[60]; // Using "course_name" avoids ambiguity with generic "name" int credit_hours; } Course;
- When passing structs to functions, remember you have two options:
- Pass by value: Sends a copy of the struct (good for small structs)
void print_course_details(Course course) { printf("Course ID: %s\nName: %s\nCredits: %d\n", course.course_id, course.course_name, course.credit_hours); } - Pass by pointer: Sends the struct's memory address (more efficient for large structs)
void print_course_details(Course *course) { printf("Course ID: %s\nName: %s\nCredits: %d\n", course->course_id, course->course_name, course->credit_hours); }
- Pass by value: Sends a copy of the struct (good for small structs)
3. Quick Tip for Reading the File Correctly
One common pitfall with your file format is that course names have spaces — you can't use scanf("%s") to read them directly. Instead, use a format specifier that captures all characters until it hits the credit hour number:
FILE *file_ptr = fopen("courses.txt", "r"); if (!file_ptr) { perror("Failed to open course file"); return 1; } Course current_course; // Read course ID, then all text until a number, then the credit hour while (fscanf(file_ptr, "%s %[^0-9] %d", current_course.course_id, current_course.course_name, ¤t_course.credit_hours) == 3) { // Pass the struct to your print function here print_course_details(current_course); } fclose(file_ptr);
内容的提问来源于stack exchange,提问作者J. Weikert

