【问题标题】:When defining new type require you to have an alias name for it to compile?当定义新类型时需要你有一个别名来编译它?
【发布时间】:2021-09-09 19:11:22
【问题描述】:

我从 Edubaca 获得此代码

在第 12 行,Edubaca 描述了 Course 取了别名 course,这是什么原因,为什么是这样的语法? 另外,第 4 行的课程是否必要?

我尝试过的:

-删除第 13 行
- 将第 4 行和第 9 行中的 Course 更改为 course

#include <stdio.h>//Add all the basic C language libraries
#include <string.h>//Add the String library to perform string actions
//typedef for give struct keyword to user wanted keyword as like below (Courses)
typedef struct Course {
    char courseName[60];//declare character variable
    float CourseFee;//declare float variable
    char companyName[100];//declare character variable
    int loginID;//declare integer variable
} Course; //To make work user defined keyword we have call the keyword from here
//main method to execute application code
int main( ) {
    //Taken Courses name as course( alias name)
    Course course;
    //Copying character values into varaible
    strcpy(course.courseName, "C Programming");
    strcpy(course.companyName, "EDUCBA");
    //Initailize float values into varaible
    course.CourseFee = 5000.00;
    //Initailize integer values into varaible
    course.loginID=2452;
    //display the output of all the declared variable below
    printf( "Course Name : %s\n", course.courseName);
    printf( "Company Name : %s\n", course.companyName);
    printf( "Course Fee : %f\n", course.CourseFee);
    printf( "Login ID : %d\n", course.loginID);
    return 0;
}

【问题讨论】:

  • 这里的“别名”应该是什么意思? Course course; 创建了一个 Course 类型的变量 - 关于它的“别名”评论非常混乱
  • @Anique 我什么都不懂。

标签: c typedef


【解决方案1】:

不,你不必typedef 一个结构并给它一个新名称,使用关键字struct 和所谓的“结构标签”就足够了。在示例中,标签是Course,因此如果没有typedef,该类型将被称为struct Course

使用typedef 可以让您抽象出类型的确切细节,这可能有用但不是必需的,它只是语言的一个特性。

你可以使用任何类型,例如

typedef unsigned short uint16_t;

如果您知道 short 是特定编译器的正确大小,那么这将是创建 C99 &lt;stdint.h&gt; 类型 uint16_t 的一种方式,依此类推。

线

Course course;

只定义了一个名为course 类型为Course 的变量,即结构的一个实例。如果没有typedef,你会写

struct Course course;

【讨论】:

    【解决方案2】:

    你可以只使用结构而不使用 typedef 和别名

    #include <stdio.h>//Add all the basic C language libraries
    #include <string.h>//Add the String library to perform string actions
    
    //typedef for give struct keyword to user wanted keyword as like below (Courses)
    struct Course {
        char courseName[60];//declare character variable
        float CourseFee;//declare float variable
        char companyName[100];//declare character variable
        int loginID;//declare integer variable
    };
    
    //main method to execute application code
    int main( ) {
        //Taken Courses name as course( alias name)
        struct Course course;
        //Copying character values into varaible
        strcpy(course.courseName, "C Programming");
        strcpy(course.companyName, "EDUCBA");
        //Initailize float values into varaible
        course.CourseFee = 5000.00;
        //Initailize integer values into varaible
        course.loginID=2452;
        //display the output of all the declared variable below
        printf( "Course Name : %s\n", course.courseName);
        printf( "Company Name : %s\n", course.companyName);
        printf( "Course Fee : %f\n", course.CourseFee);
        printf( "Login ID : %d\n", course.loginID);
        return 0;
    }
    

    【讨论】:

    • 为什么“应该”?为结构创建typedef 是一种非常常见的模式,听起来更像是你想写“可以”
    • 是的很常见,更多的是不用别名编译
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多