[英]How to write char[] into struct?
我有这样的结构名称
#define SETA_NAME "SETA"
#define SETB_NAME "SETB"
#define SETC_NAME "SETC"
#define SETD_NAME "SETD"
#define SETE_NAME "SETE"
#define SETF_NAME "SETF"
这样的结构
struct set
{
char name[SET_NAME_LENGTH + 1];
unsigned int input[INPUT_ARR_SIZE];
};
typedef struct set SETA, SETB, SETC, SETD, SETE, SETF;
这是我尝试执行的代码
SETA setA;
memcpy(setA.name, SETA_NAME, sizeof(SETA_NAME));
SETB setB;
memcpy(setA.name, SETB_NAME, sizeof(SETB_NAME));
SETC setC;
memcpy(setA.name, SETC_NAME, sizeof(SETC_NAME));
SETD setD;
memcpy(setA.name, SETD_NAME, sizeof(SETD_NAME));
SETE setE;
memcpy(setA.name, SETE_NAME, sizeof(SETE_NAME));
SETF setF;
memcpy(setA.name, SETF_NAME, sizeof(SETF_NAME));
printf("\nNAME : %s", setA.name);
printf("\nNAME : %s", setB.name);
printf("\nNAME : %s", setC.name);
printf("\nNAME : %s", setD.name);
printf("\nNAME : %s", setE.name);
printf("\nNAME : %s", setF.name);
但输出不像我期望的那样
NAME : SETF
NAME :
NAME :
NAME :
NAME : d?v
NAME :
我做错了什么?
您每次都复制到同一个结构中。
memcpy(setA.name, SETA_NAME, sizeof(SETA_NAME));
^^^^^^^^^
memcpy(setA.name, SETB_NAME, sizeof(SETB_NAME));
^^^^
memcpy(setA.name, SETC_NAME, sizeof(SETC_NAME));
^^^^
memcpy(setA.name, SETD_NAME, sizeof(SETD_NAME));
^^^^
memcpy(setA.name, SETE_NAME, sizeof(SETE_NAME));
^^^^
memcpy(setA.name, SETF_NAME, sizeof(SETF_NAME));
^^^^
您可能对初始化结构感兴趣。 在 C 中(以及在广泛增强的方式中,在 C++ 中)结构可以用花括号中的值列表进行初始化。 每个值代表一个成员; 最后缺少的成员用 0 初始化(这意味着一对空的花括号用 0 初始化每个成员)。
对于字符数组,可以使用字符串文字进行初始化,因此您可以将预定义的字符串作为初始值设定项。 我写了一个小测试程序用于演示。 享受。
#include<stdio.h>
// give the array a reasonable size.
#define SET_NAME_LENGTH 80
#define INPUT_ARR_SIZE 4
#define SETA_NAME "SETA"
#define SETB_NAME "SETB"
#define SETC_NAME "SETC"
#define SETD_NAME "SETD"
struct set
{
char name[SET_NAME_LENGTH + 1];
unsigned int input[INPUT_ARR_SIZE];
};
//
// why?
// typedef struct set SETA, SETB, SETC, SETD, SETE, SETF;
int main()
{
// The first improvement: Do value initialization.
// Each name array is initialized with the characters from the
// supplied character string literal.
// All integers in the int array are initialized with 0 in all structs below:
struct set setA = { SETA_NAME, {0,0,0,0} }; // verbose
struct set setB = { SETB_NAME, {0} }; // less verbose: omitted members are zeroed.
struct set setC = { SETC_NAME, {} }; // even less verbose: omitted members are zeroed.
struct set setD = { SETD_NAME }; // terse: omitted members are zeroed recursively.
struct set empty = {}; // Convenient way to zero out everything.
printf("\nNAME : %s", setA.name);
printf("\nNAME : %s", setB.name);
printf("\nNAME : %s", setC.name);
printf("\nNAME : %s\n", setC.name);
// Next improvement: Make it an array, because an array can be looped.
// Note the nested curly braces to initialize the structs in the array.
struct set setArr[] =
{
{ {'o', 'h', '!', '\0'} }, // character array initialized with single chars
{ SETA_NAME },
{ SETB_NAME },
{ SETC_NAME },
{ SETD_NAME },
{ "made up name"}, // extra members!
{ "Last not least name"},
};
// This is an idiom: You calculate the number of elements in an array
// by dividing the overall array size by the size of one member.
// The beauty is that we don't have to change anything when the array size changes.
// Alas, it does not work with pointers.
const int structCount = sizeof(setArr)/sizeof(*setArr);
printf("Loop:\n");
for(int structInd=0; structInd < structCount; structInd++)
{
printf("NAME : ->%s<-\n", setArr[structInd].name);
}
// The name is the empty string, first char (like all others) is '\0'.
printf("empty NAME : ->%s<-\n", empty.name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.