簡體   English   中英

如何將 char[] 寫入結構體?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM