[c/c++] programming之路(26)、結構體

1、初始化字符串

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct stu {
    char name[20];
    int age;
};

void main() {
    struct stu s1;//C版本
    stu s2 ;//C++版本
    //s2.name = "safdg";    字符串不能夠用賦值號賦值
    //初始化字符串,兩種方式:1.sprintf(stdio);2.strcpy(string)
    sprintf(s2.name, "yincheng");
    strcpy(s1.name, "zhangxin");
    printf("%s\n", s1.name);
    getchar();
}
#include<stdio.h>
#include<stdlib.h>

struct ours {
    int num;
    char str[100];//str是指針常量
};

void main() {
    struct ours o1 = { 100,"hello china" };
    struct ours o2 = o1;//結構體直接賦值的時候,即便是字符串也能夠賦值
    //o2.str = o1.str;//字符串不能直接賦值

    //字符串拷貝的方式
    sprintf(o2.str, o1.str);
    strcpy(o1.str, o2.str);

    printf("%d,%s\n", o2.num,o2.str);

    system("pause");
}

結構體內再定義結構體

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

struct china {
    int data;
    char name[100];
    struct beijing {//結構體內部再定義結構體,可是沒有建立結構體的實例
                    //再次定義的結構體內部的變量,會被當作母結構體的成員變量
        char str[100];
        int num;
    };
};

void main() {
    struct china t1;
    t1.data = 100;
    t1.num = 99;
    //t1.beijing;    報錯,不能這樣使用
    sprintf(t1.name, "goA");
    sprintf(t1.str, "goB");
    printf("%d,%d,%s,%s\n",t1.data,t1.num,t1.name,t1.str);

    system("pause");
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

struct china {
    int data;
    char name[100];
    struct beijing {//結構體內部再次定義結構體,建立結構體變量,該變量會直接做爲一個成員
        char str[100];
        int num;
    }b1;//內部定義的第一種方式
    struct beijing b2;//內部定義的第二種方式
};

void main() {
    struct china t2;
    t2.b1.num = 100;//結構體嵌套用多個點訪問
    sprintf(t2.b1.str, "ABC");
    printf("%d,%s\n",t2.b1.num,t2.b1.str);

    t2.b2.num = 100;
    sprintf(t2.b2.str, "B2_ABC");
    printf("%d,%s\n", t2.b2.num, t2.b2.str);

    system("pause");
}

定義並初始化結構體數組

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

struct data{
    int num;
    float f1;
    char str[4];
}db[3] = { {1,1.0,"1.0"},{ 2,2.0,"2.0" },{ 3,3.0,"3.0" }};

//結構體數組在內存裏面是挨個挨個排列的

void main() {
    printf("%d\n", sizeof(struct data));
    printf("%x\n", db);//打印第一個元素地址
    printf("%x\n", &db[0]);//打印三個元素地址
    printf("%x\n", &db[1]);
    printf("%x\n",&db[2]);
    system("pause");
}

結構體數組引用

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct dangdang{
    char email[30];
    char name[30];
    char addr[100];
    int num;
    int bignum;
    char tel[30];
    char phone[30];
    double RMB;
}dbdd[4]={{"asd@qq.com","顧士夫","上海市信息村",131,3,"6421475","13158474123",12.3},
    {"zxc@qq.com","水電費","綿陽市程序村",132,3,"6421475","13111111111",12.3},
    {"weq@qq.com","請問","北京市中心村",133,3,"6421475","13122222222",12.3},
    {"hjjkhk@qq.com","預定單","復古風市給的村",134,3,"6421475","13133333333",12.3},
};

void main() {
    char str[50];
    scanf("%s",str);
    printf("你要查找的是%s    \n",str);
    int num=sizeof(dbdd)/sizeof(struct dangdang);//求出數組大小
    for (int i = 0; i < num; i++)
    {
        if(strcmp(str,dbdd[i].phone)==0){
            printf("找到了,郵件爲%s,姓名爲%s,地址爲%s    \n",dbdd[i].email,dbdd[i].name,dbdd[i].addr);
        }
    }
    system("pause");
} 

 2、指向結構體的指針

指針與結構體

#include<stdio.h>
#include<stdlib.h>

struct mydata{
    int num;
    char str[100];
};

void main() {
    struct mydata md;//實例化結構體
    md.num=99;
    sprintf(md.str,"gogogo");
    printf("%d,%s\n",md.num,md.str);

    struct mydata *p;//p存儲地址,struct mydata決定了大小以及如何解析
    printf("%d\n",sizeof(p));//指針有幾個字節
    p=&md;//結構體指針初始化,存儲地址

    //修改指針指向的數據
    (*p).num=1234;
    sprintf(p->str,"wangwangwang");
    printf("%d,%s\n",(*p).num,(*p).str);
    printf("%d,%s\n",p->num,p->str);

    //指針訪問結構體的兩種方式:    (*p)    p->
    system("pause");
}

指向結構體數組的指針

#include<stdio.h>
#include<stdlib.h>

struct dangdang{
    char email[30];
    char name[30];
    char addr[100];
    int num;
    int bignum;
    char tel[30];
    char phone[30];
    double RMB;
}dbdd[4]={{"asd@qq.com","顧士夫","上海市信息村",131,3,"6421475","13158474123",12.3},
    {"zxc@qq.com","姚遠超","綿陽市程序村",132,3,"6421475","13111111111",12.3},
    {"weq@qq.com","王明","北京市中心村",133,3,"6421475","13122222222",12.3},
    {"hjjkhk@qq.com","崔帥帥","杭電",134,3,"6421475","13133333333",12.3},
};

void main0() {
    int a[5]={1,2,3,4,5};
    for (int *p = a; p < a+5; p++)//p++根據類型自增,此處一次增長四個字節,輪詢內存地址
    {
        printf("%d,%x\n",*p,p);
    }
    system("pause");
}

void main(){
    for (struct dangdang *p = dbdd;p < dbdd+4; p++)
    {
        printf("name=%s,addr=%s,phone=%s\n",p->name,(*p).addr,(*p).phone);
    }
    system("pause");
}

用指向結構體的指針做函數參數

#include<stdio.h>
#include<stdlib.h>

struct dangdang{
    char name[30];
    int num;
};

void change(int num){//值傳遞:新建一個變量接受實際參數的值
    num=1000;
}
void changeA(struct dangdang ddt){//依舊是值傳遞:使用結構體做爲參數,浪費內存,須要新建一個變量
    ddt.num=1000;
    printf("changeA=%x,%x\n",&ddt,&ddt.num);
}

void main(){
    struct dangdang ddt;
    printf("main=%x,%x\n",&ddt,&ddt.num);
    ddt.num=100;
    sprintf(ddt.name,"ABCD");
    //change(ddt.num);
    changeA(ddt);

    printf("%d\n",ddt.num);
    system("pause");
}

void changeB(struct dangdang *p){//地址傳遞
    (*p).num=1000;
}

//change(ddt.num);  changeA(ddt);
changeB(&ddt);

#include<stdio.h>
#include<stdlib.h>

struct dangdang{
    char name[30];
    int num;
};

void datatest(struct dangdang ddd[10]){//對於數組來講,參數傳遞的是地址
    printf("datatest=%d\n",sizeof(ddd)); //地址佔四個字節
    struct dangdang dd[10];//數組
    printf("datatest dd=%d\n",sizeof(dd)); 
}

void main(){
    struct dangdang ddd[10];
    struct dangdang *p=ddd;
    printf("main=%d\n",sizeof(ddd)); 
    datatest(ddd);
    system("pause");
}

#include<stdio.h>
#include<stdlib.h>

struct dangdang{
    char name[30];
    int num;
};

void dtc(struct dangdang ddd[10]){
    ddd[4].num=88;
    sprintf(ddd[4].name,"98765"); 
}

void main(){
    struct dangdang ddd[10];
    ddd[4].num=90;
    sprintf(ddd[4].name,"ABCD");

    struct dangdang *p=ddd;
    dtc(p);
    printf("%d,%s\n",ddd[4].num,ddd[4].name); 
    system("pause");
}

內存動態分配

   //struct dangdang dd[1024*1024*1024];//直接報錯,error:數組太大
    //struct dangdang dd[1024*1024];//運行報錯
    //處理大數據,必須在堆裏
    struct dangdang *p=(struct dangdang *)malloc(sizeof(struct dangdang)*1024*1024*10);
#include<stdio.h>
#include<stdlib.h>

struct dangdang{
    char email[30];
    char name[30];
    char addr[100];
    int num;
    int bignum;
    char tel[30];
    char phone[30];
    double RMB;
};

//動態分配內存,分配一個元素
void main0(){
    //處理大數據,必須在堆裏
    struct dangdang *p=(struct dangdang *)malloc(sizeof(struct dangdang));
    sprintf(p->email,"yincheng@qq.com");
    sprintf(p->name,"yincheng");
    sprintf(p->addr,"清華大學");
    p->num=100;
    p->bignum=1000;
    sprintf(p->tel,"0108848");
    sprintf(p->phone,"12345678911");
    p->RMB=45.67;
    printf("%s,%s,%s,%d,%d,%s,%s,%f\n",p->email,p->name,p->addr,p->num,p->bignum,p->tel,p->phone,p->RMB);
    system("pause");
}

//動態分配內存,分配N個元素
int Size=5;
void main(){
    struct dangdang *p=(struct dangdang *)malloc(sizeof(struct dangdang)*Size);

    //數組訪問方式
    for (int i = 0; i < Size; i++)
    {
        //寫數據
        sprintf(p[i].email,"yincheng@qq.com");
        sprintf(p[i].addr,"清華大學");
        p[i].num=100;
        //讀數據
        printf("%s,%s,%d\n",p->email,p->addr,p->num);
    }
    printf("------------------------\n");

    //指針訪問方式
    for (int i = 0; i < Size; i++)
    {
        //p[i]等價於一個結構體    p[i]=(*(p+i)),後者多加一個括號是避免優先級的歧義
        sprintf((*(p+i)).email,"yincheng@qq.com");
        sprintf((*(p+i)).addr,"清華大學");
        (*(p+i)).num=100;
        printf("%s,%s,%d\n",(*(p+i)).email,(*(p+i)).addr,(*(p+i)).num);
    }
    printf("------------------------\n");

    //指針輪詢方式
    for (struct dangdang *px=p; px < p+Size; px++)
    {
        sprintf(px->email,"yincheng@qq.com");
        sprintf(px->addr,"清華大學");
        px->num=100;
        printf("%s,%s,%d\n",px->email,px->addr,px->num);
    }

    system("pause");
}

相關文章
相關標籤/搜索