不含指针的结构体赋值实现,直接内存拷贝
#include <stdio.h>
#include <stdlib.h>
struct A {
int c;
char b[20];
char strs[10][10];
int k;
};
int main() {
// A a = {...}; // 帮我初始化
struct A a = {
1, // Initialize c with some integer
"Example String", // Initialize b with a string (fewer than 20 characters)
{ // Initialize strs with strings (each fewer than 10 characters)
"String1", // First string
"String2", // Second string
"String3", // Third string
// You can initialize more or leave them empty which will default to zeros
},
42 // Initialize k with some integer
};
struct A b;
b = a;
printf("b.strs[0]=%s\n", b.strs[0]);
}
看下 b = a 的反汇编结果:
struct A b;
b = a;
009C18E0 mov ecx,20h
009C18E5 lea esi,[a]
009C18EB lea edi,[b]
009C18F1 rep movs dword ptr es:[edi],dword ptr [esi]
printf("b.strs[0]=%s\n", b.strs[0]);
sizeof(A) = 128 == 20h*4(dword)

浙公网安备 33010602011771号