• 结构体的赋值


    结构体的赋值

           1.指针的赋值

             区分对指针本身赋值对指针指向的空间进行赋值

           2.数组的赋值

             不能对数组名进行赋值,对数组的赋值实质上是数组中元素一个一个的拷贝。

           3.结构体的赋值

             结构体中的指针

             结构体中含有指针时,对结构体赋值类似于指针的赋值,只是浅赋值,如果想深赋值,还需要额外处理。

             结构体中的数组

             结构体含有数组时,对结构体进行赋值,可以实现数组的直接赋值,而外部的数组不能进行直接赋值,而是一个一个的拷贝。

             结构体的直接赋值,实质上是对结构体的完全拷贝,不管结构体中有指针还是数组,拷贝的内容都是结构体本身,也就是说结构体中有指针时,拷贝的是指针本身;结构体中有数组时,拷贝的是数组。

             对结构体赋值,可以将结构体内部中的数组进行直接赋值。而直接的数组不能实现数组间的赋值操作。

             测试代码和注释:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct st1
    {
        char* p; // 指针
    };
    
    void print_st1(struct st1* ps)
    {
        printf("%s
    ", ps->p);
    }
    
    struct st2
    {
        char a[5]; // 数组
    };
    
    void print_st2(struct st2* ps)
    {
        printf("%s
    ", ps->a);
    }
    
    int main()
    {
        // 指针的赋值
        char  p[] = "abc";
        char* q   = p; // 对指针赋值
    
        printf("%s
    ", p);
        printf("%s
    ", q);
        printf("
    ");
    
        strcpy(p, "xyz");
    
        printf("%s
    ", p);
        printf("%s
    ", q);
        printf("
    ");
    
        // 分配空间
        q = (char*)malloc(strlen(p) + 1);
        strcpy(q, "abc");
        printf("%s
    ", p);
        printf("%s
    ", q);
        printf("
    ");
    
        free(q);
    
        // 数组的赋值
        int a[3] = {1, 2, 3};
        // int b[3] = a; // 对数组这种初始化失败
        int b[3];
        // b = a; // 赋值失败,因为不存在对数组的赋值,b是数组名,是一个常量
    
        // 拷贝a中的元素到b中
        int i = 0;
        for (i = 0; i < sizeof (a) / sizeof (*a) && sizeof (b) >= sizeof(a); ++i)
        {
            b[i] = a[i]; // 一个一个拷贝
        }
    
        for (i = 0; i < sizeof (b) / sizeof (*b); ++i)
        {
            printf("%d ", b[i]);
        }
        printf("
    ");
        printf("
    ");
    
        
        // 结构体的赋值
        // 结构体中的指针
        struct st1 s1;
        s1.p = (char*)malloc(5);
        strcpy(s1.p, "abc");
    
        struct st1 s2;
        s2 = s1; // 结构体的赋值,结构体中的指针
    
        printf("%s
    ", s1.p);
        printf("%s
    ", s2.p);
        printf("
    ");
    
        strcpy(s1.p, "xyz");
        printf("%s
    ", s1.p);
        printf("%s
    ", s2.p);
        printf("
    ");
    
        strcpy(s1.p, "abc");
        s2.p = (char*)malloc(5);
        strcpy(s2.p, s1.p);
        printf("%s
    ", s1.p);
        printf("%s
    ", s2.p);
        printf("
    ");
    
        strcpy(s2.p, "xyz");
        printf("%s
    ", s1.p);
        printf("%s
    ", s2.p);
        printf("
    ");
    
        free(s1.p);
        free(s2.p);
    
        printf("%d
    ", (int)s1.p);
        printf("%d
    ", (int)s2.p);
        printf("
    ");
        s1.p = NULL;
        s2.p = NULL;
        printf("%d
    ", (int)s1.p);
        printf("%d
    ", (int)s2.p);
        printf("
    ");
    
        // 结构体的赋值
        // 结构体中的数组
        struct st2 t1;
        strcpy(t1.a, "abc"); // 不能对数组进行直接赋值,所以只能逐个拷贝
    
        struct st2 t2;
        t2 = t1; // 结构体的赋值,结构体中的数组
                 // 结构体中有数组,如果对结构体进行赋值,那么其里面的数组也被拷贝,可以看做是数组的赋值
                 // 但是对于外部的数组直接赋值是不可以的
    
        printf("%s
    ", t1.a);
        printf("%s
    ", t2.a);
        printf("
    ");
    
        strcpy(t1.a, "xyz");
        printf("%s
    ", t1.a);
        printf("%s
    ", t2.a);
        printf("
    ");
    
        strcpy(t1.a, "abc");
        strcpy(t2.a, "xyz");
        printf("%s
    ", t1.a);
        printf("%s
    ", t2.a);
        printf("
    ");
    
        return 0;
    }

  • 相关阅读:
    BZOJ 3132: 上帝造题的七分钟 树状数组+差分
    PAT Advanced 1006 Sign In and Sign Out (25 分)
    PAT Advanced 1011 World Cup Betting (20 分)
    PAT Basic 1032 挖掘机技术哪家强 (20 分)
    PAT Basic 1028 人口普查 (20 分)
    PAT Basic 1004 成绩排名 (20 分)
    大数据数据库HBase(二)——搭建与JavaAPI
    PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)
    PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)
    PAT Basic 1041 考试座位号 (15 分)
  • 原文地址:https://www.cnblogs.com/unixfy/p/3489627.html
Copyright © 2020-2023  润新知