c - 如何在结构中存储多数据值并使用它们?

标签 c struct

我当前的代码:

#include <stdio.h>
#include <string.h>
/*
    struct Value {
       int typ;
       unsigned char vstring;
       int   vint;
       float  vfloat;
    };
*/
struct Value {
   int typ;
   /*
   type=1  ==> get vstring
   type=2  ==> get int
   type=3  ==> get float
   */
   union{
      struct{
         unsigned char *vstring;
      };
      struct{
         int   vint;
      };
      struct{
         float  vfloat;
      };
   }
};

void clear(Value vall){
   if(vall.typ == 1){
      delete(vall.vstring);
   }else if(vall.typ == 2){
      delete(vall.vint);
   }else{
      delete(vall.vfloat);
   }
}
int main()
{
   struct Value v;
   /////////////////////////////////////////////
   v.typ=1;
   strcpy( v.vstring,"C Programming/may this a very big utf-8 string!");
   /*
   strcpy( v.vint,4); 
   strcpy( v.vfloat,4.5);
   */
   /////////////////////////////////////////////
   printf( "ValueType : %d\n", v.typ);
   printf( "ValueString : %s\n", v.vstring);
   printf( "ValueInt : %d\n", v.vint);
   printf( "ValueFloat : %f\n", v.vfloat);
   return 0;
   Value copy=v;
   clear(copy);
   copy.typ=2;
   copy.vint=5;
}

但这有错误,我不知道如何解决这个问题。

它有一个 Value 结构。其中有 (vstring,vint,vfloat) ,并且值的类型存储在 typ 中以提高速度。

请帮我修复此代码。

我想将此结构存储在数组/映射/ HashMap 中...... 坦克你。

最佳答案

首先,您可以考虑使用 union 。这不是必需的,但会节省一些字节:

struct Value {
   int typ;
   /*
   type=1  ==> get vstring
   type=2  ==> get int
   type=3  ==> get float
   */
   union {
    unsigned char *vstring;  // Changed to char pointer!!
    int   vint;
    float  vfloat;
   };
};

接下来,复制字符串时,需要分配空间:

const char *msg = "C Programming/may this a very big utf-8 string!";
struct Value v;
v.typ = 1;
v.vstring = malloc(strlen(msg) + 1);
strcpy(v.vstring, msg);
// strdup() does the same thing

当你清除时,你需要释放该内存:

void clear(struct Value* vall){
   if (vall->typ == 1) {
      free(vall->vstring);
   }
   memset(vall, 0, sizeof(*vall));
}

最后,您可以使用枚举作为类型,而不是“魔数(Magic Number)”:

enum VTYPE {VSTRING, VINT, VFLOAT};
typedef enum VTYPE vtype;

此外,这里还有一个要打印的辅助函数:

void print_value(struct Value *v)
{
    static const char *types[] = {"VSTRING", "VINT", "VFLOAT"};
    printf( "ValueType : %s\n", v->typ >= 0 && v->typ < 3 ? types[v->typ] : "ERROR");
    if (VSTRING == v->typ) {
        printf( "ValueString : %s\n", v->vstring);
    }
    else if (VINT == v->typ) {
        printf( "ValueInt : %d\n", v->vint);
    }
    else if (VFLOAT == v->typ) {
        printf( "ValueFloat : %f\n", v->vfloat);
    }
}

这是整个内容的链接:http://ideone.com/ecy3qa

复制时,请务必复制字符串:

void copy_value(struct Value *source, struct Value *dest) {
    if (VSTRING == source->typ) {
        dest->vstring = malloc(strlen(source->vstring) + 1);
        strcpy(dest->vstring, source->vstring);
    }
    else if (VINT == source->typ) {
        dest->vint = source->vint;
    }
    else if (VFLOAT == source->typ) {
        dest->vfloat = source->vfloat;
    }
    dest->typ = source->typ;
}

关于c - 如何在结构中存储多数据值并使用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43494008/

相关文章:

c - 我的 switch 功能出了什么问题?

c - 尝试传递递归函数来填充二维数组时出现段错误

c - 初始化包含指向数组的指针的结构

c - API 返回结构

c++ - nvcc、gcc、clang 和 msvc "respect"在结构中使用 __restrict__ 关键字吗?

c# - 这里发生了什么?没有默认构造函数时如何调用?

c - C代码缩进练习

c - 如果 -8 & 7 不为 0,为什么输出为 False?

c - 整数变量 :3;

c# - 从顺序文件读取到结构数组