C语言学习之玩转结构体

  • 定义结构体变量并初始化:
  • 示例代码:
#include 
#include 
#include 


int main(int argc, char const *argv[])
{
    //定义类型的同时,定义变量
    struct K_DATA{


        int a;
        char b[10];
        short c;


    }t1;

    t1.a = 666;
    strcpy(t1.b,"FEFE");
    t1.c = 101;

    printf("t1.a = %d,t1.b = %s,t1.c = %d\n",t1.a,t1.b,t1.c);

    //定义类型的同时,定义变量,并初始化

    struct K_DATA1{


        int a;
        char b[10];
        short c;


    }t2 = {888,"AUAU",102};

    printf("t2.a = %d,t2.b = %s,t2.c = %d\n",t2.a,t2.b,t2.c);


    typedef struct K_DATA2{


        int a;
        char b[10];
        short c;


    }t3;

    t3 m;
    m.a = 666;
    strcpy(m.b,"FEFE");
    m.c = 101;
    printf("m.a = %d,m.b = %s,m.c = %d\n",m.a,m.b,m.c);

    typedef struct K_DATA3{


        int a;
        char b[10];
        short c;


    }t4;

    t4 n = {888,"AUAU",102};
    printf("n.a = %d,n.b = %s,n.c = %d\n",n.a,n.b,n.c);
    
    
    return 0;
}

  • 运行结果:
	t1.a = 666,t1.b = FEFE,t1.c = 101
	t2.a = 888,t2.b = AUAU,t2.c = 102
	m.a = 666,m.b = FEFE,m.c = 101
	n.a = 888,n.b = AUAU,n.c = 102
  • 定义结构体数组并初始化:
  • 示例代码:
#include 
#include 
#include 

struct K_DATA{


        int a;
        char b[10];
        short c;


};


int main(int argc, char const *argv[])
{
    
    struct K_DATA k[3] = {{666,"zhang",101},
                             {667,"li",102},
                           {668,"wang",103}};
    
    for(int i = 0; i < 3; i++){

       printf("k[%d].a = %d,k[%d].b = %s,k[%d].c = %d\n",\
                   i, k[i].a,i,k[i].b,i,k[i].c);

    }
    
    puts("--------------------------------------------------");
    
    typedef struct K_DATA2{


        int a;
        char b[10];
        short c;


    }t3;

    t3 m[3];
    m[0].a = 666;
    strcpy(m[0].b,"zhang");
    m[0].c = 101;

    m[1].a = 667;
    strcpy(m[1].b,"li");
    m[1].c = 102;

    m[2].a = 668;
    strcpy(m[2].b,"wang");
    m[2].c = 103;

    for(int i = 0; i < 3; i++){

       printf("m[%d].a = %d,m[%d].b = %s,m[%d].c = %d\n",\
                   i, m[i].a,i,m[i].b,i,m[i].c);



    }


    puts("--------------------------------------------------");

    typedef struct K_DATA3{


        int a;
        char b[10];
        short c;


    }t4;

    t4 n[3] = {{666,"zhang",101},
               {667,"li",102},
               {668,"wang",103}};
    
    for(int i = 0; i < 3; i++){

       printf("n[%d].a = %d,n[%d].b = %s,n[%d].c = %d\n",\
                   i, n[i].a,i,n[i].b,i,n[i].c);

    }
   
    
    return 0;
}

  • 运行结果:
k[0].a = 666,k[0].b = zhang,k[0].c = 101
k[1].a = 667,k[1].b = li,k[1].c = 102
k[2].a = 668,k[2].b = wang,k[2].c = 103
--------------------------------------------------
m[0].a = 666,m[0].b = zhang,m[0].c = 101
m[1].a = 667,m[1].b = li,m[1].c = 102
m[2].a = 668,m[2].b = wang,m[2].c = 103
--------------------------------------------------
n[0].a = 666,n[0].b = zhang,n[0].c = 101
n[1].a = 667,n[1].b = li,n[1].c = 102
n[2].a = 668,n[2].b = wang,n[2].c = 103
  • 结构体所有成员清0:
  • 需要调用C语言的库函数memset()
  • 该函数功能是:复制字符c指针str所指向地址的前n个字符
void *memset(void *str, int c, size_t n)
  • 示例代码:
#include 
#include 
#include 

typedef struct K_DATA{


        int a;
        char b[10];
        short c;
        char *s;


}t1;


int main(int argc, char const *argv[])
{   
    
    
    t1 k[3] = {{666,"zhang",101},
                  {667,"li",102},
                {668,"wang",103}};
    
    for(int i = 0; i < 3; i++){

       printf("k[%d].a = %d,k[%d].b = %s,k[%d].c = %d\n",\
                   i, k[i].a,i,k[i].b,i,k[i].c);

    }
    
    memset(k,0,sizeof(k));

    puts("--------------------------------------------------");

    for(int i = 0; i < 3; i++){

       printf("k[%d].a = %d,k[%d].b = %s,k[%d].c = %d\n",\
                   i, k[i].a,i,k[i].b,i,k[i].c);

    }


    puts("--------------------------------------------------");

    struct K_DATA *p = (struct K_DATA *)malloc(sizeof(struct K_DATA ));

    printf("p = %p\n",p);

    
    p->a = 666;
    p->s = "li";
    p->c = 101;

    printf("%d %s %d\n", p->a, p->s,p->c);

    memset(p,0,sizeof(t1));

    printf("%d %s %d\n", p->a, p->s,p->c);

    free(p);

    p = NULL;

    printf("p = %p\n",p);
   
    return 0;
}

  • 运行结果:
k[0].a = 666,k[0].b = zhang,k[0].c = 101
k[1].a = 667,k[1].b = li,k[1].c = 102
k[2].a = 668,k[2].b = wang,k[2].c = 103
--------------------------------------------------
k[0].a = 0,k[0].b = ,k[0].c = 0
k[1].a = 0,k[1].b = ,k[1].c = 0
k[2].a = 0,k[2].b = ,k[2].c = 0
--------------------------------------------------
p = 0x55ac015f3670
666 li 101
0 (null) 0
p = (nil)
  • 结构体成员在内存中的对齐:
  • PC为32位系统:
  • 情况1:所有的成员都小于4个字节,按照最大的成员所占内存空间大小对齐;
  • 情况2:有成员大于或者等于4个字节,按照4个字节大小对齐;
  • 注意:
  • char(占1个字节)和short(占2个字节)连续存储的情况;
  • PC为64位系统:
  • 在32位系统的基础上,按照最大的成员所占内存空间大小对齐;
  • 结构体嵌套结构体对齐的情况:
  • 示例代码:
#include 

struct K1{
    short a;
    short b;
    short c;
};

struct K2{
    struct K1 d;
    char e;
    int f;
};

struct K3{
    int a;
    short b;
};

struct K4{
    struct K3 d;
    char e;
    int f;
};

int main(int argc, const char *argv[])
{
    
    
    
    printf("sizeof(struct K2) = %d\n", sizeof(struct K2));
    struct K2 m;
    printf("&m.d.a = %p\n", &m.d.a);
    printf("&m.d.b = %p\n", &m.d.b);
    printf("&m.d.c = %p\n", &m.d.c);

    printf("&m.d = %p\n", &m.d);
    printf("&m.e = %p\n", &m.e);
    printf("&m.f = %p\n", &m.f);

    puts("-----------------------------------------------");
    printf("sizeof(struct K4) = %d\n", sizeof(struct K4));
    struct K4 n;
    printf("&n.d.a = %p\n", &n.d.a);
    printf("&n.d.b = %p\n", &n.d.b);


    printf("&n.d = %p\n", &n.d);
    printf("&n.e = %p\n", &n.e);
    printf("&n.f = %p\n", &n.f);

    return 0;
}
  • 运行结果:
sizeof(struct K2) = 12
&m.d.a = 0xffb61d80
&m.d.b = 0xffb61d82
&m.d.c = 0xffb61d84
&m.d = 0xffb61d80
&m.e = 0xffb61d86
&m.f = 0xffb61d88
-----------------------------------------------
sizeof(struct K4) = 16
&n.d.a = 0xffb61d8c
&n.d.b = 0xffb61d90
&n.d = 0xffb61d8c
&n.e = 0xffb61d94
&n.f = 0xffb61d98
  • 结构体成员位域:
  • 可以应用到压缩结构体,比如将一个字节拆成八个比特位使用;
  • 示例代码:
#include 

typedef struct Test
{
    unsigned char k1:1;
     unsigned char k2:1;
      unsigned char k3:1;
       unsigned char k4:1;
        unsigned char k5:1;
         unsigned char k6:1;
          unsigned char k7:1;
           unsigned char k8:1;
}t1;


int main(int argc, char const *argv[])
{
    printf("sizeof(t1) = %ld\n",sizeof(t1));
    return 0;
}

  • 运行结果:
sizeof(t1) = 1
  • 结构体成员含函数指针的情况:
  • 示例代码:
#include 


int my_mul(int x,int y){


    return x * y;
}

int my_div(int x,int y){


    return x / y;
}


typedef struct K_DATA{

    int(*p)(int,int);
        
}t1;


int main(int argc, char const *argv[])
{
    t1 m;
    m.p = my_mul;
    t1 n;
    n.p = my_div;
    printf("my_mul = %d,my_div = %d\n",m.p(15,15),n.p(10,2));

    return 0;
}
  • 运行结果:
my_mul = 225,my_div = 5

你可能感兴趣的:(c语言,学习,开发语言)