小编Chi*_*pyZ的帖子

C语言中结构复制的行为

我一直在阅读这个网站上关于如何在C中复制结构的一些问题.我一直在玩一些代码,试图理解'浅'复制之间的区别(新结构只是指向一个指针)第一个结构的内存地址)和"深度"复制(其中数据逐个成员地复制到新的内存块中).

我创建了以下代码,假设它将显示"浅"复制行为:

#include <stdio.h>

struct tester
{
    int blob;
    int glob;
    char* doob[10];
};

int main (void)
{
    //initializing first structure
    struct tester yoob;
    yoob.blob = 1;
    yoob.glob = 2;
    *yoob.doob = "wenises";

    //initializing second structure without filling members
    struct tester newyoob;
    newyoob = yoob; 
    //assumed this line would simply copy the address pointed to      by 'yoob'

    //printing values to show that they are the same initially
    printf("Before modifying:\n");
    printf("yoob blob: %i\n", yoob.blob);
    printf("newyoob blob: %i\n", newyoob.blob);

    //modifying 'blob' in …
Run Code Online (Sandbox Code Playgroud)

c struct

4
推荐指数
1
解决办法
186
查看次数

标签 统计

c ×1

struct ×1