[c/c++] programming之路(28)、結構體存儲和內存對齊+枚舉類型+typedef+深拷貝和淺拷貝

1、結構體存儲

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

struct info{
    char c;        //1    2    4    8    
    double num;    //1    2    4    8        char short int double
    char ch[9];    //9    10    12    16

};

void main() {
    printf("%d\n",sizeof(struct info));
    struct info in={'a',5.2,"hello"};
    printf("%p\n",&in);
    printf("%p\n",&in.c);
    printf("%p\n",&in.num);
    printf("%p\n",&in.ch);

    system("pause");
}

2、枚舉類型(限定取值)

枚舉常量實質是整型數據數組

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

//枚舉的通常形式,限定在這個範圍內取值
//若是沒有一個賦初值,就會從0循環到最後一個,每次加1
//若是第一個賦初值,後面每一個加1
//除非本身賦值,不然計算機賦值會讓每一個枚舉常量都不一樣
enum level{
    司令=5,軍長=5,師長,旅長,團長,營長,連長,排長,班長,士兵
};

void main() {
    enum level l1=司令;
    enum level l2=軍長;
    enum level l3=師長;
    printf("%d\n",l1);
    printf("%d\n",l2);
    printf("%d\n",l3);

    system("pause");
}

3、typedef

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

typedef int aa;            //typedef沒有建立數據類型,給已經有的數據類型起一個別名.編譯時處理,僅適用於類型
#define zhengshu num    //define是替換,預處理,適用於任何場合

void main() {
    aa zhengshu=10;
    printf("%d\n",num);
    system("pause");
}

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

typedef int I;//給int一個別稱
typedef int* IP;
void main() {
    I num=100;//int num=100
    IP p=&num;//int *p=&num
    printf("%d,%d\n",num,*p);
    printf("%p,%p\n",&num,p);
    system("pause");
}

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

void main() {
    /*int a[10];
    int s[10];*/
    typedef int s[10];//重定義數組類型
    s x;
    for (int i = 0; i < 10; i++)
    {
        x[i]=i;
        printf("%d\n",x[i]);
    }
    system("pause");
}

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

struct info{
    char name[100];
    int num;
};

typedef struct info I;//I=struct info
typedef struct info* P;//P=struct info*

void main() {
    I s;
    strcpy(s.name,"yincheng");
    s.num=100;
    printf("%s,%d\n",s.name,s.num);

    P ip=(P)malloc(sizeof(I));
    strcpy(ip->name,"yincheng8848");
    ip->num=8888;
    printf("%s,%d\n",(*ip).name,ip->num);
  free(ip); system(
"pause"); }

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

typedef struct tel{
    char name[30];
    long long num;
}T,*P;

void main() {
    printf("%d\n",sizeof(struct tel));
    printf("%d\n",sizeof(T));
    printf("%d\n",sizeof(P));

    T t1;
    strcpy(t1.name,"yincheng");
    t1.num=18288889999;
    printf("%s,%lld\n",t1.name,t1.num);

    P pt1=(P)malloc(sizeof(T));//malloc以後記得要free
    strcpy(pt1->name,"尹成");
    pt1->num=18611118888;
    printf("%s,%d\n",(*pt1).name,pt1->num);
    free(pt1);

    system("pause");
}

4、深拷貝和淺拷貝

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

typedef struct string{
    char *p;
    int len;
}S;

//淺拷貝,共享內存
void main1() {
    S s1,s2;
    s1.len=10;
    s1.p=(char *)malloc(sizeof(char)*10);
    strcpy(s1.p,"hello");
    printf("s1.p=%s\n",s1.p);
    s2.len=s1.len;
    s2.p=s1.p;
    *(s1.p)='K';
    printf("s2.p=%s\n",s2.p);

    system("pause");
}
//深拷貝,拷貝內存內容
void main() {
    S s1,s2;
    s1.len=10;
    s1.p=(char *)malloc(sizeof(char)*10);
    strcpy(s1.p,"hello");
    printf("s1.p=%s\n",s1.p);
    s2.len=s1.len;
    s2.p=(char *)malloc(sizeof(char)*10);
    strcpy(s2.p,s1.p);
    *(s1.p)='K';
    printf("s2.p=%s\n",s2.p);

    system("pause");
}

淺拷貝spa

深拷貝3d

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息