Path: ...!feeds.phibee-telecom.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams Newsgroups: comp.lang.c Subject: "not-const" qualifier for C Date: Wed, 24 Apr 2024 16:24:52 -0300 Organization: A noiseless patient Spider Lines: 65 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 24 Apr 2024 21:24:53 +0200 (CEST) Injection-Info: dont-email.me; posting-host="fbf65ac619c5165ee777e0c9ede4f805"; logging-data="2631401"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zS63nfTtUJtEHUFEzkG3CDF4zvwiuIcA=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:oxVZz4xLdvK/b0rdu8sBaNZf8RQ= Content-Language: en-US Bytes: 1966 Motivation sample: struct X { const char* const type; }; struct X * make_x(){ struct X * p = malloc(sizeof *p); if (p) { p->type = strdup("X"); // *** error, type is const *** if (p->type == NULL) { free(p); p = NULL; } } return p; //ok } void print(struct X * p){ prinf("%s", p->type); } void x_destroy(struct X * p) { free(p->type); // *** warning const to non-const *** } Now consider we have the keyword "mutable" that removes const from struct members. struct X { const char* const type; }; struct X * make_x(){ mutable struct X * p = malloc(sizeof *p); if (p) { p->type = strdup("X"); //OK if (p->type == NULL) { free(p); p = NULL; } } return p; //ok } void print(struct X * p){ prinf("%s", p->type); } void x_destroy(mutable struct X * p) { free(p->type); //no warning }