0

如何在结构中保存一个常量值?如果我将 const 放在 LEBEL0 上,我将无法在 LEBEL1 上分配给它。但是,如果我不在 LEBEL0 处放置 const,那么我将在 LEBEL1 处收到限定符丢失警告。有没有办法做到这一点?我想出了一个肮脏的解决方案(如下),但我认为可能会有更好的解决方案......

    typedef struct _MyStruct
    {
        const SomePointerType pktData; <--LABEL0
    }MyStruct;

    const SomePointerType SomeFunctionICannotModify()
    {
    …
    }

    void SomeFunction()
    {
       MyStruct data;
    ….
       data.pktData = SomeFunctionICannotModify(); <--LABEL1
   ....
       SomeSubFunction(&data);
    }

    void SomeSubFunction(MyStruct* data)
    {
        this function does not modify any fields in "data".
    }

PS:上面的代码是一个“模型”,不是真实的代码,来说明我的问题。它没有我实际程序中的所有代码。请不要问诸如“你为什么要那样做”、“你不必那样做”、“那个代码不能编译”之类的问题。

我的肮脏解决方案

    typedef struct _ConstHolder
    {
        const SomePointerType pktData;
    }ConstHolder;

    typedef struct _MyStruct
    {
        ConstHolder holder;
    }MyStruct;

    void SomeFunction()
    {
       MyStruct data;
    ….
       ConstHolder holder = {SomeFunctionICannotModify()};
       data.holder = holder;
   ....
       SomeSubFunction(&data);
    }
4

2 回答 2

0

It looks to me like the problem is that you are using typedef along with const. The thing about C is that const is not as well implemented and usable as it is in C++. And typedef is not well done either.

This article talks a bit about const in C. As does this article as well on const in C.

For instance if you use the following, it will compile with no problems in Visual Studio 2005

typedef char * pchar;

typedef struct {
    int i;
    int j;
} Thing;

typedef Thing * SomePtr;

typedef struct {
//  const SomePtr pSome;
//  const pchar   mychar;
    const char * mychar;
    const Thing *pSome;
} MyStruct;

const SomePtr SomePtrFunc ()
{
    static Thing jj = {1, 2};

    return &jj;
}

int main(int argc, char **argv)
{
    MyStruct josey;

    josey.pSome = SomePtrFunc();

    josey.mychar = "this";
    return 0;
}

If I were to then try to modify what is pointed to by josey.pSome by doing something like adding a line of code such as josey.pSome->i = 0; then I will see a compilation error of error C2166: l-value specifies const object.

However if you use the following code with comments switched around, you get an error message of error C2166: l-value specifies const object

typedef char * pchar;

typedef struct {
    int i;
    int j;
} Thing;

typedef Thing * SomePtr;

typedef struct {
    const SomePtr pSome;
    const pchar   mychar;
//  const char * mychar;
//  const Thing *pSome;
} MyStruct;

const SomePtr SomePtrFunc ()
{
    static Thing jj = {1, 2};

    return &jj;
}

int main(int argc, char **argv)
{
    MyStruct josey;

    josey.pSome = SomePtrFunc();  // <- error C2166: l-value specifies const object

    josey.mychar = "this";        // <- error C2166: l-value specifies const object
    return 0;
}

Finally what you could do is to make the typedef to include the const qualifier which would then make the const qualifier part of the type:

typedef const char * pchar;

typedef struct {
    int i;
    int j;
} Thing;

typedef const Thing * SomePtr;

typedef struct {
    SomePtr pSome;
    pchar   mychar;
//  const char * mychar;
//  const Thing *pSome;
} MyStruct;
于 2014-02-07T03:37:53.770 回答
0

您的问题很难理解,但是您可能会误解为在分配给 pktData 后无法对其进行修改。这不是真的。考虑以下内容,这是完全合法的:

const char * str = "hello";
printf("%s\n", str);
str = "goodbye";
printf("%s\n", str);

const 并不是说​​您不能重新分配指针,而是说您不能更改它指向的数据。

于 2014-02-07T01:59:54.957 回答