在没有任何函数的情况下,如何在C中为结构中的数组赋值?

在没有任何函数的情况下,如何在C中为结构中的数组赋值?,c,C,如果没有任何函数,例如strcpy(),我如何编写下面的程序? 我知道我们可以用函数来做,但我想知道如何不用函数来做 #include <stdio.h> #include <string.h> struct name { char fname[100]; }; struct list { struct name m; }; int main() { struct list l; strcpy(l.m.fname,"hello");

如果没有任何函数,例如
strcpy()
,我如何编写下面的程序? 我知道我们可以用函数来做,但我想知道如何不用函数来做

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

struct name
{
    char fname[100];
};

struct list
{
    struct name m;
};


int main()
{
    struct list l;
    strcpy(l.m.fname,"hello");
}
#包括
#包括
结构名
{
char-fname[100];
};
结构列表
{
结构名m;
};
int main()
{
结构列表l;
strcpy(l.m.fname,“你好”);
}

我认为这几乎需要写出函数本身背后的代码。或者你可以硬编码每个值。

你几乎只需要扩展strcpy中的代码,这只是简单的循环复制每个字符,直到它到达结尾的空终止符

{
  struct list l;
  const char *in = "hello";
  char *out = l.m.fname;
  while (*in)
    *out++ = *in++;
  }
  *out=0;
}
使用C99(或更新版本):

这是一个好主意。看到了

请注意,它只在数组位于
结构内部时才起作用,就像在原始代码中一样,因为C不允许直接赋值给数组


尽管如此,
strcpy
可能会稍微快一点,因为它只复制6个字节,而上面的复合文字赋值复制100个字节。

你知道
strcpy
实际上是做什么的吗?(如果是这样,自己编写应该不难,特别是如果字符串总是“hello”)
struct name nm={“hello”};l、 m=nm
l.m=(结构名){“hello”}你是对的,但是有一个原因标准循环是
while((*out++=*in++)!='\0')
while(*out++=*in++)具有空循环体和条件中的复制-在循环后没有额外的赋值,它更紧凑。
l.m = (struct name){"hello"};