亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++中typedef 及其與struct的結(jié)合使用

 更新時(shí)間:2014年02月21日 15:20:55   作者:  
這篇文章主要介紹了C++中typedef 及其與struct的結(jié)合使用,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

//相當(dāng)于為現(xiàn)有類型創(chuàng)建一個(gè)別名,或稱類型別名。
//整形等
typedef int size;


//字符數(shù)組
char line[81];
char text[81];//=>

typedef char Line[81];
Line text, secondline;


//指針
typedef char * pstr;
int mystrcmp(pstr p1, pstr p2);//注:不能寫成int mystrcmp(const pstr p1, const pstr p3);因const pstr p1解釋為char * const cp(不是簡(jiǎn)單的替代)


//與結(jié)構(gòu)類型組合使用
typedef struct tagMyStruct
{
int iNum;
long lLength;
} MyStruct;//(此處MyStruct為結(jié)構(gòu)類型別名)=>

struct tagMyStruct
{
int iNum;
long lLength;
};//+
typedef struct tagMyStruct MyStruct;


//結(jié)構(gòu)中包含指向自己的指針用法
typedef struct tagNode
{
char *pItem;
pNode pNext;
} *pNode;//=>error
//1)
typedef struct tagNode
{
char *pItem;
struct tagNode *pNext;
} *pNode;
//2)
typedef struct tagNode *pNode;
struct tagNode
{
char *pItem;
pNode pNext;
};
//3)規(guī)范
struct tagNode
{
char *pItem;
struct tagNode *pNext;
};
typedef struct tagNode *pNode;


//與define的區(qū)別
//1)
typedef char* pStr1;//重新創(chuàng)建名字
#define pStr2 char *//簡(jiǎn)單文本替換
pStr1 s1, s2;
pStr2 s3, s4;=>pStr2 s3, *s4;
//2)define定義時(shí)若定義中有表達(dá)式,加括號(hào);typedef則無需。
#define f(x) x*x=>#define f(x) ((x)*(x))
main( )
{
int a=6,b=2,c;
c=f(a) / f(b);
printf("%d \\n",c);
}
//3)typedef不是簡(jiǎn)單的文本替換
typedef char * pStr;
char string[4] = "abc";
const char *p1 = string;
const pStr p2 = string;=>error
p1++;
p2++;

//1) #define宏定義有一個(gè)特別的長(zhǎng)處:可以使用 #ifdef ,#ifndef等來進(jìn)行邏輯判斷,還可以使用#undef來取消定義。
//2) typedef也有一個(gè)特別的長(zhǎng)處:它符合范圍規(guī)則,使用typedef定義的變量類型其作用范圍限制在所定義的函數(shù)或者文件內(nèi)(取決于此變量定義的位置),而宏定義則沒有這種特性。

復(fù)制代碼 代碼如下:

//
//C中定義結(jié)構(gòu)類型
typedef struct Student
{
int a;
}Stu;//申明變量Stu stu1;或struct Student stu1;
//或
typedef struct
{
int a;
}Stu;//申明變量Stu stu1;

//C++中定義結(jié)構(gòu)類型
struct Student
{
int a;
};//申明變量Student stu2;


//C++中使用區(qū)別
struct Student
{
int a;
}stu1;//stu1是一個(gè)變量 。訪問stu1.a

typedef struct Student2
{
int a;
}stu2;//stu2是一個(gè)結(jié)構(gòu)體類型 訪問stu2 s2; s2.a=10;
//還有待增加。

相關(guān)文章

最新評(píng)論