新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > struct和typedef struct

struct和typedef struct

作者: 時間:2016-11-27 來源:網(wǎng)絡 收藏

第二篇:在C和C++中struct和typedef struct的區(qū)別


在C和C++有三種定義結(jié)構(gòu)的方法。

typedef struct {

int data;

int text;

} S1;

//這種方法可以在c或者c++中定義一個S1結(jié)構(gòu)

struct S2 {

int data;

int text;

};

// 這種定義方式只能在C++中使用,而如果用在C中,那么編譯器會報錯

struct {

int data;

int text;

} S3;

這種方法并沒有定義一個結(jié)構(gòu),而是定義了一個s3的結(jié)構(gòu)變量,編譯器會為s3內(nèi)存。

void main()

{

S1 mine1;// OK ,S1 是一個類型

S2 mine2;// OK,S2 是一個類型

S3 mine3;// OK,S3 不是一個類型

S1.data = 5;// ERRORS1 是一個類型

S2.data = 5;// ERRORS2 是一個類型

S3.data = 5;// OKS3是一個變量

}

另外,對與在結(jié)構(gòu)中定義結(jié)構(gòu)本身的變量也有幾種寫法

struct S6 {

S6* ptr;

};

// 這種寫法只能在C++中使用

typedef struct {

S7* ptr;

} S7;

// 這是一種在C和C++中都是錯誤的定義

如果在C中,我們可以使用這樣一個“曲線救國的方法“

typedef struct tagS8{

tagS8 * ptr;

} S8;

第三篇:struct和typedef struct

分三塊來講述:
1 首先:
在C中定義一個結(jié)構(gòu)體類型要用typedef:
typedef struct Student
{
int a;
}Stu;
于是在聲明變量的時候就可:Stu stu1;
如果沒有typedef就必須用struct Student stu1;來聲明
這里的Stu實際上就是struct Student的別名。
另外這里也可以不寫Student(于是也不能struct Student stu1;了)
typedef struct
{
int a;
}Stu;
但在c++里很簡單,直接
struct Student
{
int a;
};
于是就定義了結(jié)構(gòu)體類型Student,聲明變量時直接Student stu2;
===========================================
2其次:
在c++中如果用typedef的話,又會造成區(qū)別:
struct Student
{
int a;
}stu1;//stu1是一個變量
typedef struct Student2
{
int a;
}stu2;//stu2是一個結(jié)構(gòu)體類型
使用時可以直接訪問stu1.a
但是stu2則必須先 stu2 s2;
然后 s2.a=10;
===========================================
3 掌握上面兩條就可以了,不過最后我們探討個沒多大關系的問題
如果在c程序中我們寫:
typedef struct
{
int num;
int age;
}aaa,bbb,ccc;
這算什么呢?
我個人觀察編譯器(VC6)的理解,這相當于
typedef struct
{
int num;
int age;
}aaa;
typedef aaa bbb;
typedef aaa ccc;
也就是說aaa,bbb,ccc三者都是結(jié)構(gòu)體類型。聲明變量時用任何一個都可以,在c++中也是如此。但是你要注意的是這個在c++中如果寫掉了typedef關鍵字,那么aaa,bbb,ccc將是截然不同的三個對象。

第四篇:C/C++中typedef struct和struct的用法

struct _x1 { ...}x1; 和 typedef struct _x2{ ...} x2; 有什么不同?

其實, 前者是定義了類_x1和_x1的對象實例x1, 后者是定義了類_x2和_x2的類別名x2 ,

所以它們在使用過程中是有取別的.請看實例1.

[知識點]

結(jié)構(gòu)也是一種數(shù)據(jù)類型, 可以使用結(jié)構(gòu)變量, 因此, 象其它 類型的變量一樣, 在使用結(jié)構(gòu)變量時要先對其定義。

定義結(jié)構(gòu)變量的一般格式為:

struct 結(jié)構(gòu)名

{

類型 變量名;

類型 變量名;

...

} 結(jié)構(gòu)變量;

結(jié)構(gòu)名是結(jié)構(gòu)的標識符不是變量名。

另一種常用格式為:

typedef struct 結(jié)構(gòu)名

{

類型 變量名;

類型 變量名;

...

} 結(jié)構(gòu)別名;

另外注意: 在C中,struct不能包含函數(shù)。在C++中,對struct進行了擴展,可以包含函數(shù)。

======================================================================

實例1: struct.cpp

#include

using namespace std;

typedef struct _point{

int x;

int y;

}point; //定義類,給類一個別名

struct _hello{

int x,y;

} hello; //同時定義類和對象

int main()

{

point pt1;

pt1.x = 2;

pt1.y = 5;

cout<< "ptpt1.x=" << pt1.x << "pt.y=" <//hello pt2;

//pt2.x = 8;

//pt2.y =10;

//cout<<"pt2pt2.x="<< pt2.x <<"pt2.y="<//上面的hello pt2;這一行編譯將不能通過. 為什么?

//因為hello是被定義了的對象實例了.

//正確做法如下: 用hello.x和hello.y

hello.x = 8;

hello.y = 10;

cout<< "hellohello.x=" << hello.x << "hello.y=" <return 0;

}

第五篇:問答

Q:用struct和typedef struct 定義一個結(jié)構(gòu)體有什么區(qū)別?為什么會有兩種方式呢?

struct Student
{
int a;
} stu;
typedef struct Student2
{
int a;
}stu2;

A:

事實上,這個東西是從C語言中遺留過來的,typedef可以定義新的復合類型或給現(xiàn)有類型起一個別名,在C語言中,如果你使用
struct xxx
{
}; 的方法,使用時就必須用 struct xxx var 來聲明變量,而使用
typedef struct
{
}的方法 就可以寫為 xxx var;
不過在C++中已經(jīng)沒有這回事了,無論你用哪一種寫法都可以使用第二種方式聲明變量,這個應該算是C語言的糟粕。


上一頁 1 2 下一頁

關鍵詞: structtypede

評論


技術專區(qū)

關閉