C - 带有 const 运算符的警告(在结构中)



这可能是一个新手问题,但我无法弄清楚为什么我会收到警告

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("%sn",collezione[4].size);
    printf("%sn",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++; 
    }
}   

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

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

你可能不会那样做。

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

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 字符串中,文字具有非常量数组的类型。

在这里,您的函数需要一个指向 Camicia 的常量指针

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的内容时,它的意思正是如此。有些东西是常量,但你试图修改它,就好像它不是一样。

尝试理解编译器的错误消息很重要,您将节省大量时间。

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

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

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

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

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("%sn",collezione[4].size);
    printf("%sn",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];
    }
}

您正在创建指向const指针的non-const指针,您无法修改const指针指向的内容,但是如果您放弃const限定符,您可以通过新的non-const指针执行此操作,因此编译器会警告您这一点。

最新更新