【问题标题】:warning with const operator (in struct)带有 const 运算符的警告(在结构中)
【发布时间】:2015-03-06 17:23:26
【问题描述】:

这可能是一个小问题,但我不知道为什么我会收到警告

struct4.c:32:15:警告:赋值从指针目标类型中丢弃“const”限定符 [默认启用] crea[i].size = wsize[i%5];

编译:

struct shirt {
     char *size;
    char *colour;
} ;
    typedef struct shirt Camicia;

void loadshirt (Camicia * const crea, const char *wsize[] , const char *wcolour[]);


int main (void) {

    Camicia collezione[50];

    const char *sizearray[] = {"xs","s","m","l","xl"};
    const char *colourarray[] = {"black","blue","yellow","orange"};

    loadshirt(collezione,sizearray,colourarray);

    printf("%s\n",collezione[4].size);
    printf("%s\n",collezione[4].colour);

    return 0;
}

void loadshirt (Camicia * const crea, const char *wsize[] , const char *wcolour[]) {

    int i=0;

    while (i<50) {
    crea[i].size = wsize[i%5];
    crea[i].colour = wcolour[i%4];
    i++; 
    }
}   

【问题讨论】:

  • 请缩进你的代码。现在很难读。
  • 您编辑了您的问题,将Camicia const * crea 更改为Camicia * const crea。您得到的答案指出这是问题所在。是哪个?
  • 顺便说一句,您的代码在运行时会出现 SEGFAULT。

标签: c struct constants


【解决方案1】:

您将结构的数据成员定义为指向非常量字符串的指针。

struct shirt {
char *size;
char *colour;
} ;

但是在函数中,您将指向 const 字符串的指针分配给指向 non-const 字符串的指针

crea[i].size = wsize[i%5];
crea[i].colour = wcolour[i%4];

查看参数列表中 wsize 和 wcolor 的声明

const char *wsize[] , const char *wcolour[]

你不能那样做。

将数据成员定义为指向 const 字符串的指针

struct shirt {
const char *size;
const char *colour;
} ;

或者将参数定义为具有指向非常量字符串的指针类型

char *wsize[] , char *wcolour[]

在这种情况下,您还必须更改相应参数的定义

char *sizearray[] = {"xs","s","m","l","xl"};
char *colourarray[] = {"black","blue","yellow","orange"};

在 C 字符串中,字面量具有非常量数组的类型。

【讨论】:

    【解决方案2】:

    这里你的函数需要一个指向 Camicia 的 const 指针

    void loadshirt (Camicia const * crea, const char *wsize[] , const char *wcolour[]) {
    

    3 行后你尝试修改 crea :

    crea[i].size = wsize[i%5];
    crea[i].colour = wcolour[i%4];
    

    你不能那样做。 当编译器说出类似X discards 'const' qualifier 的内容时,它的意思正是如此。有些东西是 const,但你试图修改它,就好像它不是一样。

    尝试理解编译器的错误信息很重要,您会节省很多时间。

    现在如果你想修复这个函数,首先你需要从参数列表中的crea 中删除 const 限定符。

    但还要注意,这里的 wsize 和 wcolour 是 const,而 Camicia 是这样定义的:

    struct shirt {
    char *size;
    char *colour;
    } ;
    typedef struct shirt Camicia;
    

    要么让你的结构 Camicia 存储 const char*,要么将其他参数修改为 char*。由于您在 main 中使用字符串文字,您可能希望所有内容都是 const char*。

    【讨论】:

      【解决方案3】:
      to avoid the runtime seg fault events,
      the following code will work correctly.
      this code takes into account that the arrays in main()
      are actually an array of pointers to char* (I.E. strings)
      
      #include <stdio.h>
      #include <stdlib.h>
      
      #define MAX_SHIRTS (50)
      
      struct shirt {
          const char *size;
          const char *colour;
      } ;
      
      // struct shirt * const says the pointer is const, 
      // not that the contents of where the pointer points is const
      void loadshirt (struct shirt * const, const char **, const char **);
      
      
      int main (void) {
      
          struct shirt collezione[MAX_SHIRTS];
      
          // create two arrays of const pointers to consts
          const char const *pSize[]   = {"xs","s","m","l","xl"}; 
          const char const *pColour[] = {"black","blue","yellow","orange"}; 
      
          loadshirt(collezione, pSize, pColour);
      
          printf("%s\n",collezione[4].size);
          printf("%s\n",collezione[4].colour);
      
          return 0;
      }
      
      
      void loadshirt (struct shirt * const crea, const char **pSize , const char **pColour)
      {
      
          int i=0;
      
          for(i=0; i<MAX_SHIRTS; i++)
          {
              crea[i].size   = pSize[i%5];
              crea[i].colour = pColour[i%4];
          }
      }
      

      【讨论】:

        【解决方案4】:

        您正在将non-const 指针指向const 指针,您无法修改const 指针指向的内容,但如果您丢弃const 限定符,您可以通过新的@987654325 进行修改@指针,所以编译器会警告你。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-20
          • 2019-03-17
          • 2016-11-08
          • 2015-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-31
          相关资源
          最近更新 更多