You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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, or course_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 name variable (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);
      }
      

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, 
              &current_course.credit_hours) == 3) {
    // Pass the struct to your print function here
    print_course_details(current_course);
}

fclose(file_ptr);

内容的提问来源于stack exchange,提问作者J. Weikert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:35:54