本文為C語言結(jié)構(gòu)的應用總結(jié),C語言沒有類的概念,但可以通過結(jié)構(gòu)體構(gòu)建一個類似類的操作概念。下文是使用的場景代碼,由于水平有限,錯誤之處請各位大神指出。在此拋磚引玉,希望做嵌入式的的人能提供更多的應用的典型案例。
#include#include/*結(jié)構(gòu)體定義 */
//只有結(jié)構(gòu)類型的定義
struct stuff
{
? ? ? ? ? ? char job[20];
? ? ? ? ? ? int? age;
? ? ? ? ? ? float height;
};
//含有結(jié)構(gòu)體類型的結(jié)構(gòu)體變量初始化定義
struct stuff1
{
? ? ? ? ?char job[20];
? ? ? ? ? int? age;
? ? ? ? ? float height;
}Hu;
//struct stuff1? Hu //就相當于這樣子
// 結(jié)構(gòu)體不能再用來定義同類型的結(jié)構(gòu)體變量,一錘子買賣;
struct
{
? ? ?char job[20];
? ? ?int? age;
? ? float height;
}Hu1;//這種類型的結(jié)構(gòu)體變量只能有這么一個,不能去定義其他的
/*結(jié)構(gòu)體別名聲明 */
typedef struct Stu
{
? ? ? ? char job[20];
? ? ? ? ?int? age;
? ? ? ? ?float height;
}Stuff,*PtrStuff;
Stuff? ? ? Sam;//等于 struct Stu Sam;
PtrStuff? pSam;//等于 struct Stu *Sam;
/*結(jié)構(gòu)體與其他類型 */
typedef struct arraychange
{
? ? ? ? ? ? int cnt;
? ? ? ? ? ?char pc[0];
}arraychange;//pc數(shù)組只能放在最后,不能調(diào)換位置;柔性數(shù)組,少用;
struct check
{
? ? ? ? union
? ? ? ? {
? ? ? ? ? ? ? char a;
? ? ? ? ?};
? ? ? ? ?int i;
}CheckSystem; //系統(tǒng)內(nèi)存大小端測試,結(jié)構(gòu)體與聯(lián)合體結(jié)合
struct structureA
{
? ? ? ? ?struct structureB
? ? ? ? {
? ? ? ? ? ? ? ?int c;
? ? ? ? ? }b;? //必須要有實體才能夠在后面使用struct structureB bb;
? ? ? ? struct structureB bb;
}a;//結(jié)構(gòu)體中含有結(jié)構(gòu)體;
//函數(shù)可以用結(jié)構(gòu)體元素,結(jié)構(gòu)體,結(jié)構(gòu)體指針做參數(shù);用元素與普通沒什么區(qū)別,用結(jié)構(gòu)體則拷貝副本,指針是唯一的;函數(shù)可以返回結(jié)構(gòu)體,使用指針的效率高
void func1(int par);? ? ? ? //結(jié)構(gòu)體元素
Stuff func2(Stuff par);? ? ? //結(jié)構(gòu)體,返回結(jié)構(gòu)體
void func3(PtrStuff par); //結(jié)構(gòu)體指針
void print(Stuff par);
//結(jié)構(gòu)體含有函數(shù)指針做元素,這樣就可以實現(xiàn)類似面向?qū)ο蟮念惙庋b
int fun1(int age,int weight);
void fun2(int age,int weight);
struct Fun
{
? ? ? ? ? ? int age;
? ? ? ? ? ? int weight;
? ? ? ? ? ? int (*pfun1)(int age,int weight);? //函數(shù)還有一個int返回值,兩個int參數(shù)
? ? ? ? ? ?void (*pfun2)(int age,int weight);? //函數(shù)沒有返回值,兩個int參數(shù)
};
int main(void)
{
? ? ? ? ? /*結(jié)構(gòu)體作為函數(shù)參數(shù) */
? ? ? ? ? Stuff? ? test,tmptest;
? ? ? ? ? ?PtrStuff? ptest=&test;
? ? ? ? ? /*結(jié)構(gòu)體含有函數(shù)指針*/
? ? ? ? ? ?struct Fun myfun={10,45,fun1,fun2};
? ? ? ? ? ?struct Fun *ptrmyfun=&myfun;
? ? ? ? ? int age=22,weight=60,temp=0;
? ? ? ? ? ?/*結(jié)構(gòu)體作為函數(shù)參數(shù) */
? ? ? ? ? ?strcpy(test.job,"manager");
? ? ? ? ? ? test.age=25;
? ? ? ? ? ?test.height=1.65;
? ? ? ? ? func1(test.age);
? ? ? ? ? tmptest=func2(test);
? ? ? ? ? print(tmptest);
? ? ? ? ? func3(ptest);
? ? ? ? ? ?print(test);
? ? ? ? ?/*結(jié)構(gòu)體含有函數(shù)指針*/
? ? ? ? ? temp=myfun.pfun1(age,weight); //成員調(diào)用函數(shù)
? ? ? ? ? printf("temp %d\n",temp);
? ? ? ? ? ? myfun.pfun2(age,weight);? //成員調(diào)用函數(shù)
? ? ? ? ? myfun.age=80;
? ? ? ? ? ?myfun.weight=65;
? ? ? ? ? ? temp=ptrmyfun->pfun1(myfun.age,myfun.weight); //指針調(diào)用函數(shù)
? ? ? ? ?printf("temp %d\n",temp);
? ? ? ? ?ptrmyfun->pfun2(myfun.age,myfun.weight);? ? //指針調(diào)用函數(shù)
? ? ? ? return 0;
}
void print(Stuff par)
{
printf("your job is:%s\n",par.job);
printf("your age is:%d\n",par.age);
printf("your height is:%f\n",par.height);
}
void func1(int par)
{
printf("your age is:%d\n",par);
}
Stuff func2(Stuff par)
{
strcpy(par.job,"Engineer");
par.age=30;
par.height=1.80;
return par;
}
void func3(PtrStuff par)
{
strcpy(par->job,"Programmer");
par->age=19;
par->height=170;
}
int fun1(int age,int weight)
{
int temp=0;
temp=age+weight;
return temp;
}
void fun2(int age,int weight)
{
printf("your age is:%d\n",age);
printf("your weight is:%d\n",weight);
}