Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

struct and typedef in C versus C++

I am currently using a C++ IDE for something that will need to work on C, and wanted to make sure that I won't have problems with this later on. After making the struct below:

typedef struct test {
   int a;
   int b;
};

I then create an instance of it using test my_test; then stuff like my_test.a = 5, etc... and this works fine in my VStudio C++. Is this going to work on gcc later on?

I read the related questions that popped up (I see I am not the first person with this kind of question, either) but no one seemed to use the way I did.

In fact, what is the difference between typedef struct {//stuff} test; and my version?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
typedef struct THIS_IS_A_TAG
{
    int a;
    int b;
} THIS_IS_A_TYPEDEF;

THIS_IS_A_TYPEDEF object1;     // declare an object.       C:Ok,     C++:Ok
struct THIS_IS_A_TAG object2;  // declare another object.  C:Ok,     C++:Ok
THIS_IS_A_TAG object3;         // declare another object.  C:Not Ok, C++:Ok

The reason for the typedef is because C programmers would like to be able to do that third thing, but they can't.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...