Re: "not-const" qualifier for C

Liste des GroupesRevenir à l c 
Sujet : Re: "not-const" qualifier for C
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.c
Date : 25. Apr 2024, 03: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

Date Sujet#  Auteur
24 Apr 24 * "not-const" qualifier for C6Thiago Adams
25 Apr 24 +* Re: "not-const" qualifier for C3Kaz Kylheku
25 Apr 24 i`* Re: "not-const" qualifier for C2Thiago Adams
25 Apr 24 i `- Re: "not-const" qualifier for C1Kaz Kylheku
25 Apr 24 +- Re: "not-const" qualifier for C1Blue-Maned_Hawk
23 May 24 `- Re: "not-const" qualifier for C1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal