使用“=”分配带有char数组的struct?

时间:2013-07-27 12:10:12

标签: c arrays struct char

我正在审查某人的代码,开发人员已将一个结构分配给另一个。该struct包含一个char数组。不知何故,“工作”,意味着结构A中的char数组确实被复制到结构B(而不是通过引用)。我完全不知所措。可以s.o.向我解释一下?我写了一个小程序来说明“问题”。

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

/**************************************
 * some define and typedef for later
 **************************************/
#define MAX_LEN (80)

typedef struct month_s
{
    char name[MAX_LEN];
} month_st;

/**************************************
 * This bit is clear.
 **************************************/
static void usingString()
{
    char mCur[MAX_LEN] = {"Jan"};
    char mNext[MAX_LEN] = {"Feb"};

    /** this doesn't compile (of course) */
    //mCur = mNext;

    /** we need a pointer/reference */
    char *pmCur = &mCur[0];

    /** however now we "copy by reference"...*/
    pmCur = &(mNext[0]);

    /** ...so this change also applies to pmCur*/
    strcpy(mNext, "Mar");

    /** ...so pmCur is now "Mar"*/
    printf("%s: %s\n", __func__, pmCur);
}

/**************************************
 * I though the same/s.th. similar
 * as above happens here. But this "works".
 * I'm surprised to see that not to be
 * the case. Can someone explain?
 **************************************/
static void usingStruct()
{
    month_st mCur = {"Jan"};
    month_st mNext = {"Feb"};

    /** I would have thought this is "copy by reference" for the string! */
    mCur = mNext;

    /** I would have thought this also overrides mCur.name
            'cause it's pointing to mNext.name now */
    strcpy(mNext.name, "Mar");

    /** how come this works??? mCur.name is still "Feb"*/
    printf("%s: %s\n", __func__, mCur.name);
}
/**************************************
 * just the main()
 **************************************/
int main()
{
    usingString();
    usingStruct();
    return 0;
}

4 个答案:

答案 0 :(得分:3)

你可能最惊讶的是数组和指针在C中是不一样的。在你的例子中的数组成员不仅包含指向字符串的指针而且包含字符串本身。因此,当您将一个struct实例分配给另一个时,会复制所有这些。

答案 1 :(得分:3)

我看到关于指针和数组之间差异的三个解释,但它们似乎没有解决被问到的问题,正如我所理解的那样。

正如我所理解的那样,问题是:“我知道我不能将一个数组分配给另一个,所以为什么可以如果数组在一个结构中,我会这么做?” / p>

答案基本上是“因为语言这么说”。尽管结构是一种聚合类型,并且很可能包含一个数组,但语言规范说结构是可分配的,因此结构是可分配的。这是N1256N1570中的6.5.16.1 Simple assignment部分。

(在引擎盖下,编译器可能必须实现内存副本,但这是编译器的问题,而不是你的问题。)

答案 2 :(得分:2)

根据你的结构声明:

typedef struct month_s
{
    char name[MAX_LEN];
} month_st;

sizeof(month_st)包含作为结构成员的char数组的空格。(注意你正在进行静态内存分配,name不是指针而是数组)。
因此,当您将一个strct变量分配给其他变量时,它会完全复制数组(或者我们可以说将复制总sizeof(month_st))个字节)。

接下来,您将声明两个结构变量:

month_st mCur = {"Jan"};
month_st mNext = {"Feb"}; 

变量mCurmNext的记忆不同。当您执行赋值nCur = mNext;时,mNext的值将复制到nCur结构变量的内存,但两者都具有单独的(自己的)内存。

strcpy()声明:

strcpy(mNext, "Mar");

仅影响变量mNext,它不会更改变量nCur的内容。

如果您感到困惑,假设您已按如下方式声明了结构:

typedef struct month_s
{
    char* name;
} month_st; 

仍然通过执行nCur = mNext;sizeof(month_st))字节从mNext复制到nCur变量,因此只复制到name的地址是指向内存的指针。

在这种情况下,完整的数组/内存(您可能动态分配)不复制它称为浅拷贝

答案 3 :(得分:0)

您很困惑,因为您认为mCurmNext是指向对象的指针,而实际上它们是对象。结构month_s仅在内存中分配空间以存储月份名称。它没有为存储指向任何东西的指针分配内存。因此,当mCur被赋值为mNext时,整个对象将按照值复制而不是按引用复制。

为方便起见,我注释了您的代码:

static void usingStruct()
{
    month_st mCur = {"Jan"};
    month_st mNext = {"Feb"};

    /** mCur and mNext are both objects. Assigning one to the other 
      * will copy by value and not by reference (as there is no reference
      * to be copied in the first place). After this assignment, 
      * mCur == {"Feb"}  */
    mCur = mNext;

    /** mNext.name is the address of the memory allocated to the object
      * mNext. This line copies the characters "Mar" to the first three
      * bytes of this memory allocation */
    strcpy(mNext.name, "Mar");

    /** At this point, mCur == {"Feb"} and mNext == {"Mar"} */
    printf("%s: %s\n", __func__, mCur.name);
}
相关问题