简体   繁体   English

C struct-合法成员?

[英]C struct - legal members?

Is this legal and/or good practice? 这是法律和/或良好做法吗?

#define SOFTWARE_VERSION_NUMBER  "7.0v1.1" 

Want struct to always contain the version number. 希望struct始终包含版本号。

typedef struct {
    char SOFTWARE_VERSION_NUMBER; 
    int a;
    int b;
    int c;
}mystruct;

No, that's not legal C. 不,那不是合法的C。

The best way to do this is to create a function to generate new instances of your struct, and put the assignment in there: 最好的方法是创建一个函数来生成结构的新实例,并将赋值放在其中:

#define SOFTWARE_VERSION_NUMBER  "7.0v1.1"

typedef struct {
    char ver[sizeof SOFTWARE_VERSION_NUMBER]; 
    int a;
    int b;
    int c;
} mystruct;

mystruct *mystruct_new(int a, int b, int c)
{
    mystruct *ms = malloc(sizeof *ms);

    if (ms)
    {
        strcpy(ms->ver, SOFTWARE_VERSION_NUMBER);
        ms->a = a;
        ms->b = b;
        ms->c = c;
    }

    return ms;
}

Your string macro cannot be stored in a single char. 您的字符串宏不能存储在单个字符中。 You would need a char * or char[strlen(SOFTWARE_VERSION_NUMBER)] buffer. 您将需要一个char *或char [strlen(SOFTWARE_VERSION_NUMBER)]缓冲区。

typedef struct _mystruct_t
{
    char version[10];
    int etc;
} mystruct_t;

mystruct_t ms;
strcpy(ms.version, SOFTWARE_VERSION_NUMBER);

No, it's not legal. 不,这是不合法的。

You could, however, do: 但是,您可以这样做:

#define SW_VERSION "1.01"
typedef struct _foo {
 char ver[sizeof SW_VERSION];
 int a;
 int b;
 int c;
} foo;

foo bar={SW_VERSION,1,2,3};

Call me crazy, but as a developer who cut his teeth on embedded systems with far less than 640K of memory , I cringe every time I see a #define 'd string. 叫我疯了,但是作为一个开发者,他在内存不足640K的嵌入式系统上不遗余力,每次看到# #define字符串时,我都会感到畏缩。 Without changing the default settings, the compiler may create a new instance of the string—and potentially allocate memory for that new instance—every time you use the macro. 在不更改默认设置的情况下,每次您使用宏时,编译器都可以创建字符串的新实例,并可能为该新实例分配内存。

An alternative, which allocates the string only once: 另一种方法,仅分配一次字符串:

const char * const MyVersion = "7.0v1.1"  // Const ptr to const string

typedef struct _foo_t {
   const char *ver;
   int a;
   int b;
   int c;
} foo_t;

foo_t bar = { MyVersion, 1, 2, 3 };  // Copy the ptr, not the string

Not legal as typed. 输入的内容不合法。

If you want to store a version number I recommend encoding it into a 32 bit int and filling it in at struct allocation time. 如果要存储版本号,建议将其编码为32位int并在结构分配时将其填写。

Might I ask why you want to store this in a struct? 我可能会问为什么要将此存储在结构中? Is it being sent across a network? 是否通过网络发送?

As far storage goes, compilers (or linkers, I'm not sure) can store the same string in one place in the data section if the same exact string is used more than once so using the macro isn't a bad thing. 就存储而言,如果多次使用相同的确切字符串,则编译器(或链接器,我不确定)可以将同一字符串存储在数据部分的某个位置,因此使用宏并不是一件坏事。 Personally I would do something like this: 我个人会做这样的事情:

const char *GetSoftwareVersion (void)
{
    return "Version 7.0.1";
}

If it is for a plugin-like DLL architecture, the function version is the most appopriate (such as the following: 如果它是用于类似插件的DLL体系结构,则函数版本是最合适的(例如:

const char *pluginVer = dll->GetSoftwareVersion(); // where GetSoftwareVersion is of type:
typedef const char *(* GetSoftwareVersionProc)(void);

Here's one approach, which fixes everything at compile time: 这是一种在编译时修复所有问题的方法:

/* -------------------------------------------------- */
/* Version.h */
#define SOFTWARE_VERSION_NUMBER "7.0v1.1"

/* -------------------------------------------------- */
/* Global.h */
#define SoftwareVersionLENGTH   8
extern const char Global_SoftwareVersion[SoftwareVersionLENGTH];

/* -------------------------------------------------- */
/* Global.c */
#include "Global.h"
#include "Version.h"
const char Global_SoftwareVersion[SoftwareVersionLENGTH]
    = SOFTWARE_VERSION_NUMBER;

If the version number needs to be changed, only Version.h must be edited (assuming the version string doesn't get any longer). 如果需要更改版本号,则仅必须编辑Version.h(假定不再获得版本字符串)。

The constant string Global_SoftwareVersion can then be referenced, consistently, in the code. 然后可以在代码中一致地引用常量字符串Global_SoftwareVersion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM