Sujet : Re: "not-const" qualifier for C
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 25. Apr 2024, 02:36:07
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <29954376-ec3c-41ca-b1c7-27b4faab6625@gmail.com>
References : 1 2
User-Agent : Mozilla Thunderbird
Em 4/24/2024 8:13 PM, Kaz Kylheku escreveu:
On 2024-04-24, Thiago Adams <thiago.adams@gmail.com> wrote:
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
}
Different idea: allow all conversions without a cast which only
add qualifiers anywhere in the type:
struct X {
const char* const type;
};
struct mutable_X {
char* type;
};
In this case the types struct X and struct mutable_X are not convertible. They are not the same type.
I forgot to put a sample but
void f(struct X x);
mutable struct X x;
f(x); //ok from mutable to const is fine