Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"May the forces of evil become confused on the way to your house." -- George Carlin


devel / comp.lang.c / Re: Why does C allow structs to have a tag?

SubjectAuthor
* Why does C allow structs to have a tag?James Harris
+* Re: Why does C allow structs to have a tag?David Brown
|`* Re: Why does C allow structs to have a tag?Thiago Adams
| `- Re: Why does C allow structs to have a tag?David Brown
+- Re: Why does C allow structs to have a tag?Öö Tiib
+* Re: Why does C allow structs to have a tag?Bart
|+* Re: Why does C allow structs to have a tag?Kaz Kylheku
||+- Re: Why does C allow structs to have a tag?Bart
||`* Re: Why does C allow structs to have a tag?Manfred
|| `* Re: Why does C allow structs to have a tag?David Brown
||  `* Re: Why does C allow structs to have a tag?Bart
||   +- Re: Why does C allow structs to have a tag?Richard Harnden
||   +* Re: Why does C allow structs to have a tag?Keith Thompson
||   |+* Re: Why does C allow structs to have a tag?Bart
||   ||+- Re: Why does C allow structs to have a tag?Keith Thompson
||   ||`* Re: Why does C allow structs to have a tag?James Harris
||   || `- Re: Why does C allow structs to have a tag?Bart
||   |`* Re: Why does C allow structs to have a tag?Manfred
||   | `* Re: Why does C allow structs to have a tag?Keith Thompson
||   |  `* Re: Why does C allow structs to have a tag?Bart
||   |   +* Re: Why does C allow structs to have a tag?David Brown
||   |   |`- Re: Why does C allow structs to have a tag?Bart
||   |   `* Re: Why does C allow structs to have a tag?Keith Thompson
||   |    `- Re: Why does C allow structs to have a tag?Malcolm McLean
||   `* Re: Why does C allow structs to have a tag?David Brown
||    `* Re: Why does C allow structs to have a tag?Bart
||     +* Re: Why does C allow structs to have a tag?David Brown
||     |`* Re: Why does C allow structs to have a tag?Bart
||     | `* Re: Why does C allow structs to have a tag?Malcolm McLean
||     |  `* Re: Why does C allow structs to have a tag?Bart
||     |   `* Re: Why does C allow structs to have a tag?Kaz Kylheku
||     |    `* Re: Why does C allow structs to have a tag?Bart
||     |     +- Re: Why does C allow structs to have a tag?James Harris
||     |     `* Re: Why does C allow structs to have a tag?Kaz Kylheku
||     |      `* Re: Why does C allow structs to have a tag?Bart
||     |       `* Re: Why does C allow structs to have a tag?Keith Thompson
||     |        `* Re: Why does C allow structs to have a tag?Bart
||     |         `- Re: Why does C allow structs to have a tag?Keith Thompson
||     `* Re: Why does C allow structs to have a tag?James Harris
||      `- Re: Why does C allow structs to have a tag?Bart
|`- Re: Why does C allow structs to have a tag?Keith Thompson
+* Re: Why does C allow structs to have a tag?Kaz Kylheku
|`* Re: Why does C allow structs to have a tag?Bart
| `- Re: Why does C allow structs to have a tag?James Harris
+- Re: Why does C allow structs to have a tag?James Kuyper
+* Re: Why does C allow structs to have a tag?Richard Damon
|`- Re: Why does C allow structs to have a tag?James Harris
+* Re: Why does C allow structs to have a tag?Joe Pfeiffer
|`* Re: Why does C allow structs to have a tag?Malcolm McLean
| +* Re: Why does C allow structs to have a tag?Guillaume
| |+* Re: Why does C allow structs to have a tag?Kaz Kylheku
| ||`* Re: Why does C allow structs to have a tag?Bart
| || `* Re: Why does C allow structs to have a tag?Kaz Kylheku
| ||  `* Re: Why does C allow structs to have a tag?Bart
| ||   `- Re: Why does C allow structs to have a tag?Keith Thompson
| |`* Re: Why does C allow structs to have a tag?James Kuyper
| | `* Re: Why does C allow structs to have a tag?Bart
| |  `* Re: Why does C allow structs to have a tag?Kaz Kylheku
| |   +* Re: Why does C allow structs to have a tag?Bart
| |   |`* Re: Why does C allow structs to have a tag?Kaz Kylheku
| |   | `* Re: Why does C allow structs to have a tag?Bart
| |   |  `* Re: Why does C allow structs to have a tag?David Brown
| |   |   `* Re: Why does C allow structs to have a tag?Bart
| |   |    +* Re: Why does C allow structs to have a tag?David Brown
| |   |    |`* Re: Why does C allow structs to have a tag?Bart
| |   |    | `* Re: Why does C allow structs to have a tag?David Brown
| |   |    |  +* Re: Why does C allow structs to have a tag?Manfred
| |   |    |  |`- Re: Why does C allow structs to have a tag?David Brown
| |   |    |  +- Re: Why does C allow structs to have a tag?Bart
| |   |    |  `* Re: Why does C allow structs to have a tag?Bart
| |   |    |   +- Re: Why does C allow structs to have a tag?David Brown
| |   |    |   `* Re: Why does C allow structs to have a tag?Keith Thompson
| |   |    |    +* Re: Why does C allow structs to have a tag?Guillaume
| |   |    |    |+* Re: Why does C allow structs to have a tag?Kaz Kylheku
| |   |    |    ||`- Re: Why does C allow structs to have a tag?Keith Thompson
| |   |    |    |+- Re: Why does C allow structs to have a tag?Keith Thompson
| |   |    |    |+- Re: Why does C allow structs to have a tag?Keith Thompson
| |   |    |    |`- Re: Why does C allow structs to have a tag?David Brown
| |   |    |    `* Re: Why does C allow structs to have a tag?Bart
| |   |    |     `* Re: Why does C allow structs to have a tag?Keith Thompson
| |   |    |      `* Re: Why does C allow structs to have a tag?Bart
| |   |    |       +* Re: Why does C allow structs to have a tag?Kaz Kylheku
| |   |    |       |`* Re: Why does C allow structs to have a tag?Ben Bacarisse
| |   |    |       | `* Re: Why does C allow structs to have a tag?Bart
| |   |    |       |  `* Re: Why does C allow structs to have a tag?Kaz Kylheku
| |   |    |       |   `* Re: Why does C allow structs to have a tag?Bart
| |   |    |       |    +* Re: Why does C allow structs to have a tag?Kaz Kylheku
| |   |    |       |    |`* Re: Why does C allow structs to have a tag?Bart
| |   |    |       |    | `- Re: Why does C allow structs to have a tag?Keith Thompson
| |   |    |       |    `- Re: Why does C allow structs to have a tag?Keith Thompson
| |   |    |       `- Re: Why does C allow structs to have a tag?David Brown
| |   |    `* Re: Why does C allow structs to have a tag?Kaz Kylheku
| |   |     `* Re: Why does C allow structs to have a tag?Bart
| |   |      `* Re: Why does C allow structs to have a tag?Kaz Kylheku
| |   |       `- Re: Why does C allow structs to have a tag?Bart
| |   `* Re: Why does C allow structs to have a tag?Bart
| |    +- Re: Why does C allow structs to have a tag?Keith Thompson
| |    `- Re: Why does C allow structs to have a tag?Kaz Kylheku
| +* Re: Why does C allow structs to have a tag?Joe Pfeiffer
| |`- Re: Why does C allow structs to have a tag?Kaz Kylheku
| `- Re: Why does C allow structs to have a tag?Richard Damon
+* Re: Why does C allow structs to have a tag?John Bode
+- Re: Why does C allow structs to have a tag?Siri Cruise
`* Re: Why does C allow structs to have a tag?Andrey Tarasevich

Pages:12345678910111213141516
Re: Why does C allow structs to have a tag?

<Xg6xI.24716$mGJ.14154@fx08.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17001&group=comp.lang.c#17001

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!feeder5.feed.usenet.farm!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx08.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <1bzgw0tpqu.fsf@pfeifferfamily.net>
<45b9fa5f-d715-451c-b236-1a5d236614f7n@googlegroups.com>
<s9o16i$133n$1@gioia.aioe.org> <s9qne6$4df$1@dont-email.me>
<J66wI.2$73h1.0@fx10.ams4> <20210609104104.187@kylheku.com>
<hv8wI.5322$rTd1.3536@fx15.ams4> <20210609135620.66@kylheku.com>
<MxbwI.2275$yVI1.1795@fx07.ams4> <s9sl65$tsq$1@dont-email.me>
<qRmwI.20038$gpy1.17527@fx11.ams4> <s9t457$53d$1@dont-email.me>
<EapwI.31173$TPRa.30348@fx01.ams4> <s9t95k$9m2$1@dont-email.me>
<UvFwI.63057$TPRa.40408@fx01.ams4> <87wnr0z0o8.fsf@nosuchdomain.example.com>
<SDQwI.44742$v0S3.2038@fx12.ams4> <87mtrwyrk7.fsf@nosuchdomain.example.com>
<7tRwI.23036$73h1.4209@fx10.ams4> <20210611160729.280@kylheku.com>
<87wnr00y56.fsf@bsb.me.uk> <XYSwI.115529$O2a8.52210@fx02.ams4>
<20210611171319.390@kylheku.com> <9I%wI.16462$yVI1.8123@fx07.ams4>
<20210612084006.621@kylheku.com>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <20210612084006.621@kylheku.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210612-0, 12/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 140
Message-ID: <Xg6xI.24716$mGJ.14154@fx08.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Sat, 12 Jun 2021 17:36:55 UTC
Organization: virginmedia.com
Date: Sat, 12 Jun 2021 18:36:54 +0100
X-Received-Bytes: 7193
 by: Bart - Sat, 12 Jun 2021 17:36 UTC

On 12/06/2021 17:12, Kaz Kylheku wrote:
> On 2021-06-12, Bart <bc@freeuk.com> wrote:

>> (1) I couldn't for the life of me figure out the type syntax.
>
> Right; that's why there is this comp.lang.c newsgroup where getting
> help with that is topical.
>
> You could make a typedef:
>
> typedef void (*any_fun_t)(int, int, int, ..., int);

Yes, that's what I did for the second example (well, I copied from yours).

> You're using a strawman example to make an unclear point.

My point (1) is that C type declarations are so crazy, they are not fit
for purpose. I shoudn't need to use external tools or workarounds like
typedefs to write a perfectly simple type that in my everyday language I
just write as '[]ref proc' (array of pointer to function).

(Has it never occurred to anyone here how absolutely bizarre it is to
have that array modifier '[]' /in the middle/ of
'void(*functable[])(void)'?)

>> typedef void(*fnptr)(int,int,int,int,int,int);
>>
>> fnptr functable[]={(fnptr)printf, (fnptr)malloc, (fnptr)fwrite};
>>
>> Except now gcc complains of casts between incompatible pointer types.
>> And it has a point.
>
> Now you're evidently just making things up:
>
> $ gcc -Wall -W -pedantic -ansi fptr.c -c
> $ gcc -Wall -W -pedantic -std=c99 fptr.c -c # silence
> $ cat fptr.c
> #include <stdio.h>
>
> void (*fptr)(void) = (void (*)(void)) printf;

I get this:

C:\c>gcc @w c.c
c.c:5:20: warning: cast between incompatible function types from 'int
(*)(const char * restrict, ..
..)' to 'void (*)(int, int, int, int, int, int)' [-Wcast-function-type]
5 | fnptr functable[]={(fnptr)printf, (fnptr)malloc, (fnptr)fwrite};
| ^
..... + 2 more identical warnings ....

'w' contains a long bunch of options I downloaded from somewhere. The
relevant one is '-Wbad-function-cast'.

But if you decide not to use it, then somebody else will, as I found out
what I used to post links to some of my generated code. Somebody would
always ramp up the warnings as a way to make out that my code was
rubbish. (The same program can generate from 0 lines of warnings, to
28,000 lines.)

However, I can use C compilers' typical laxity to my advantage, by
saying my code needs to be compiled with NO options except any necessary
ones to get the job done (eg. -O3, -m64, -o, and on Linux, -lm and -ldl).

Since apparently /I/ get to choose whether a program is correct or not
by the options I feed a compiler. So here /I/ decide that using void*
for function pointers is correct code. If someone doesn't agree, take it
up with the compiler writers.

>> void (*osnos_os_getdllprocaddr(int64_t hinst,uint8_t * name))(void) {
>> static struct osnos_os_getdllprocaddr_rec table[23] = {
>> {(uint8_t*)"malloc",(void *)(&malloc)},
>> {(uint8_t*)"realloc",(void *)(&realloc)},
>> {(uint8_t*)"free",(void *)(&free)},
>
> This is supposed to show how to do it more succinctly with fewer casts?
>
> Why would you not just use "char *", for one thing.

Because literal strings in the original language have type 'ref u8' (the
nearest C type), and the generated code has to reflect what is in the
original source.

Literal strings in C would normally have type 'ref i8', but the reality
is even weirder: they have type 'ref ?8' where ? is either i or u, but
you can't specify which as ? is compatible with neither.

>> {(uint8_t*)"printf",(void *)(&printf)},
> ^ gratuitous operator
> ^ unnecessary parens

> The cast notation and & are both unary operators: they go on the left
> side of the operand and consequently associate right to left.
>
> Parentheses are never needed to disambiguate between the same category
> of operators; that's not what they are for.

You're nitpicking my C code generator. If I try and minimise the
generation of parentheses, I'm going to get it wrong (as I found out).

But in generated code, superfluous parentheses are harmeless. (You're
seeing here the results of an experiment to use uint8_t int64_t etc
instead of u8 and i64; the visual impact of those is greater.)

>> global function os_getdllprocaddr(intm hinst,ichar name)ref proc=
>> record rec=
>> ichar name
>> ref void addr
>> end
>
> Now *that*'s a low-effort way to write a table of function pointers in C.
> Write another language first, then capture the output.

Huh?

>
> Where is the risk in any of this code that a table entry could be called
> directly?

That's an old function that used 'ref proc' as a way to indicate a
generic function pointer. It may have had something to do with problems
generating C code if I'd used 'ref void'; I don't recall.

The current version uses 'ref void' (void*). I no longer support a C target.

> The gaping risk is that something will call the function to look up
> a name, and then misuse it by casting it to the wrong type before
> calling it.

They can always cast things in both languages.

> Obtaining an address from a name with the nicest C code is the least
> important aspect of the whole interpreter design situation.

Yes, that's the easy bit. The hard bit, calling the functions, in 'C'
usually involves a complicated library called LIBFF, which only works
with gcc or compatible compilers since it uses inline ASM.

Re: Why does C allow structs to have a tag?

<87eed6zx7g.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17002&group=comp.lang.c#17002

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Sat, 12 Jun 2021 12:42:27 -0700
Organization: None to speak of
Lines: 40
Message-ID: <87eed6zx7g.fsf@nosuchdomain.example.com>
References: <s9iea5$n5c$1@dont-email.me> <J66wI.2$73h1.0@fx10.ams4>
<20210609104104.187@kylheku.com> <hv8wI.5322$rTd1.3536@fx15.ams4>
<20210609135620.66@kylheku.com> <MxbwI.2275$yVI1.1795@fx07.ams4>
<s9sl65$tsq$1@dont-email.me> <qRmwI.20038$gpy1.17527@fx11.ams4>
<s9t457$53d$1@dont-email.me> <EapwI.31173$TPRa.30348@fx01.ams4>
<s9t95k$9m2$1@dont-email.me> <UvFwI.63057$TPRa.40408@fx01.ams4>
<87wnr0z0o8.fsf@nosuchdomain.example.com>
<SDQwI.44742$v0S3.2038@fx12.ams4>
<87mtrwyrk7.fsf@nosuchdomain.example.com>
<7tRwI.23036$73h1.4209@fx10.ams4> <20210611160729.280@kylheku.com>
<87wnr00y56.fsf@bsb.me.uk> <XYSwI.115529$O2a8.52210@fx02.ams4>
<20210611171319.390@kylheku.com> <9I%wI.16462$yVI1.8123@fx07.ams4>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b3dc605b3c3d09671336ab6840e77b5e";
logging-data="1223"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SdKsoRk4EHvVm/cSaciB4"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:0LJD/PgwaNgcjQkuNGgGzNukScE=
sha1:FTnW3SJw81TrOKwt+h+GaKhgSA0=
 by: Keith Thompson - Sat, 12 Jun 2021 19:42 UTC

Bart <bc@freeuk.com> writes:
[...]
> Alternatively we can go with your idea of using a function pointer
> type that cannot be called by accident:
>
> typedef void(*fnptr)(int,int,int,int,int,int);
>
> fnptr functable[]={(fnptr)printf, (fnptr)malloc, (fnptr)fwrite};
>
> Except now gcc complains of casts between incompatible pointer
> types.

That gives me a warning only if I enable gcc's "-Wcast-function-type"
option, which is included in "-Wextra". The code is valid (though as
Kaz points out, you have to store some other information to be able to
use the pointers safely).

Compilers are allowed to warn about anything they like.

> And it has a point. It needs to go through an intermediate
> type:
>
> fnptr functable[]={(fnptr)(uintptr_t)printf,
> (fnptr)(uintptr_t)malloc, (fnptr)(uintptr_t)fwrite};

No, you don't need to go through an intermediate type. The original
code is safe and portable. Your version with the intermediate type is
not (it would fail on an implementation where a function pointer can't
be converted to uintptr_t without loss of information). You've made the
code worse for the sake of avoiding a warning.

Your void* approach was actually better than your uintptr_t approach
(but still not 100% portable, but I know you don't care about that).

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Re: Why does C allow structs to have a tag?

<87a6nuzw42.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17003&group=comp.lang.c#17003

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Sat, 12 Jun 2021 13:06:05 -0700
Organization: None to speak of
Lines: 13
Message-ID: <87a6nuzw42.fsf@nosuchdomain.example.com>
References: <s9iea5$n5c$1@dont-email.me> <hv8wI.5322$rTd1.3536@fx15.ams4>
<20210609135620.66@kylheku.com> <MxbwI.2275$yVI1.1795@fx07.ams4>
<s9sl65$tsq$1@dont-email.me> <qRmwI.20038$gpy1.17527@fx11.ams4>
<s9t457$53d$1@dont-email.me> <EapwI.31173$TPRa.30348@fx01.ams4>
<s9t95k$9m2$1@dont-email.me> <UvFwI.63057$TPRa.40408@fx01.ams4>
<87wnr0z0o8.fsf@nosuchdomain.example.com>
<SDQwI.44742$v0S3.2038@fx12.ams4>
<87mtrwyrk7.fsf@nosuchdomain.example.com>
<7tRwI.23036$73h1.4209@fx10.ams4> <20210611160729.280@kylheku.com>
<87wnr00y56.fsf@bsb.me.uk> <XYSwI.115529$O2a8.52210@fx02.ams4>
<20210611171319.390@kylheku.com> <9I%wI.16462$yVI1.8123@fx07.ams4>
<20210612084006.621@kylheku.com> <Xg6xI.24716$mGJ.14154@fx08.ams4>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b3dc605b3c3d09671336ab6840e77b5e";
logging-data="14473"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qD20SH/BNBgkv7/bIaNet"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:oLhvNR+yNB77yU8VXHo87QgrDQ4=
sha1:R1Is7J5yW4MSGLgnv1768MntORE=
 by: Keith Thompson - Sat, 12 Jun 2021 20:06 UTC

Bart <bc@freeuk.com> writes:
[...]
> (Has it never occurred to anyone here how absolutely bizarre it is to
> have that array modifier '[]' /in the middle/ of
> 'void(*functable[])(void)'?)
[...]

It's probably occurred to *everyone* here.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Re: Why does C allow structs to have a tag?

<saeclh$e0n$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17042&group=comp.lang.c#17042

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Wed, 16 Jun 2021 19:39:44 -0700
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <saeclh$e0n$1@dont-email.me>
References: <s9iea5$n5c$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 17 Jun 2021 02:39:45 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="21c6754a2b21a4147f4ecec29490a741";
logging-data="14359"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ed2IaVb7gZZDlPERy9Xsp"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.1
Cancel-Lock: sha1:gW+Czomlf3I3aU5blmTy7ONVRHA=
In-Reply-To: <s9iea5$n5c$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Thu, 17 Jun 2021 02:39 UTC

On 6/6/2021 5:16 AM, James Harris wrote:
> Does C, as a language, need to allow its structs to have tags?
>
> So could C's struct tags be omitted from the language? If not, what does
> a struct tag add? Is it something to do with forward declarations or
> syntactic consistency with union tags, etc?

The tag is absolutely necessary when you want to declare a
self-referential struct type, like a node in a linked list that must
contain a pointer to the same type.

Just try declaring a linked list node without using a tag. Report to the
forum.

--
Best regards,
Andrey Tarasevich

P.S. Of course, you can get around this by using `void *` pointers and
explicit type casts everywhere, but that would still represent a major
loss of language functionality.

Re: Why does C allow structs to have a tag?

<duFyI.21896$R591.16204@fx37.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17045&group=comp.lang.c#17045

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!feeder1.feed.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx37.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <saeclh$e0n$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210616-4, 16/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 66
Message-ID: <duFyI.21896$R591.16204@fx37.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Thu, 17 Jun 2021 10:29:29 UTC
Organization: virginmedia.com
Date: Thu, 17 Jun 2021 11:29:26 +0100
X-Received-Bytes: 2334
 by: Bart - Thu, 17 Jun 2021 10:29 UTC

On 17/06/2021 03:39, Andrey Tarasevich wrote:
> On 6/6/2021 5:16 AM, James Harris wrote:
>> Does C, as a language, need to allow its structs to have tags?
>>
>> So could C's struct tags be omitted from the language? If not, what
>> does a struct tag add? Is it something to do with forward declarations
>> or syntactic consistency with union tags, etc?
>
> The tag is absolutely necessary when you want to declare a
> self-referential struct type, like a node in a linked list that must
> contain a pointer to the same type.
>
> Just try declaring a linked list node without using a tag. Report to the
> forum.
>

It's not actually that hard, you just use void*, with the odd cast as
needed:

------------------------------------
#include <stdio.h>
#include <stdlib.h>

#define While for

typedef struct {
int data;
void* next;
} node;

node *head, *end;

void printlist(void) {
node* p=head;

While (;p; p=p->next) {
printf("%d\n",p->data);
}
}

void addnode(int x) {
node* p=malloc(sizeof(node));
p->data=x;
p->next=NULL;

if (head) {
end->next=p;
} else {
head=p;
}
end=p;
}

int main(void) {
node* p;

addnode(100);
addnode(200);
addnode(300);

printlist();
} ------------------------------------

When a cast is needed, you use (node*). No tags required. It's not as
type-safe, but then blame those stupid tags.

Re: Why does C allow structs to have a tag?

<87wnqsix3z.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17046&group=comp.lang.c#17046

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 11:50:08 +0100
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <87wnqsix3z.fsf@bsb.me.uk>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="dba4e90f2d9a52676a13a099c8050f76";
logging-data="31400"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ip5oTWf4gyK2TCl2h249Tw0iPeObrqY4="
Cancel-Lock: sha1:XlVkVdBtqFWrdEr6eAUJ3RF0RiY=
sha1:5aWn6BTEcWktzD0SmXlCKJuC2J0=
X-BSB-Auth: 1.d8f4efca7e29ebbaa798.20210617115008BST.87wnqsix3z.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 17 Jun 2021 10:50 UTC

Bart <bc@freeuk.com> writes:

> On 17/06/2021 03:39, Andrey Tarasevich wrote:
>> On 6/6/2021 5:16 AM, James Harris wrote:
>>> Does C, as a language, need to allow its structs to have tags?
>>>
>>> So could C's struct tags be omitted from the language? If not, what does a struct tag add? Is it something to do with forward declarations
>>> or syntactic consistency with union tags, etc?
>> The tag is absolutely necessary when you want to declare a
>> self-referential struct type, like a node in a linked list that must contain a pointer to the same type.
>> Just try declaring a linked list node without using a tag. Report to the
>> forum.
>
> It's not actually that hard, you just use void*, with the odd cast as needed:

> typedef struct {
> int data;
> void* next;
> } node;

> When a cast is needed, you use (node*). No tags required. It's not as
> type-safe, but then blame those stupid tags.

The natural C definition is

struct node {
int data;
struct node *next;
};

Which has served me well for decades. But, if you don't want to call
the type by its natural name, just write

typedef struct node node;
struct node {
int data;
node *next;
};

and live happily ever after. Why would anyone use void * just to avoid
a single use of a struct tag?

--
Ben.

Re: Why does C allow structs to have a tag?

<xZFyI.49758$aOx3.30095@fx22.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17047&group=comp.lang.c#17047

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx22.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <87wnqsix3z.fsf@bsb.me.uk>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <87wnqsix3z.fsf@bsb.me.uk>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210616-4, 16/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 59
Message-ID: <xZFyI.49758$aOx3.30095@fx22.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Thu, 17 Jun 2021 11:02:53 UTC
Organization: virginmedia.com
Date: Thu, 17 Jun 2021 12:02:49 +0100
X-Received-Bytes: 3032
 by: Bart - Thu, 17 Jun 2021 11:02 UTC

On 17/06/2021 11:50, Ben Bacarisse wrote:
> Bart <bc@freeuk.com> writes:
>
>> On 17/06/2021 03:39, Andrey Tarasevich wrote:
>>> On 6/6/2021 5:16 AM, James Harris wrote:
>>>> Does C, as a language, need to allow its structs to have tags?
>>>>
>>>> So could C's struct tags be omitted from the language? If not, what does a struct tag add? Is it something to do with forward declarations
>>>> or syntactic consistency with union tags, etc?
>>> The tag is absolutely necessary when you want to declare a
>>> self-referential struct type, like a node in a linked list that must contain a pointer to the same type.
>>> Just try declaring a linked list node without using a tag. Report to the
>>> forum.
>>
>> It's not actually that hard, you just use void*, with the odd cast as needed:
>
>> typedef struct {
>> int data;
>> void* next;
>> } node;
>
>> When a cast is needed, you use (node*). No tags required. It's not as
>> type-safe, but then blame those stupid tags.
>
> The natural C definition is
>
> struct node {
> int data;
> struct node *next;
> };
>
> Which has served me well for decades.

I've used linked lists without needing the concept of a 'tag' for
decades too.

But, if you don't want to call
> the type by its natural name, just write
>
> typedef struct node node;
> struct node {
> int data;
> node *next;
> };
>
> and live happily ever after. Why would anyone use void * just to avoid
> a single use of a struct tag?
>

I was responding to the "The tag is absolutely necessary ... " comment.

When I need to write actual C, then I use tags combined with typedef.
The tags are likely to be the same name as the typedef, but with a _ prefix.

When I generated C code, I used tags only without typedef.

When I don't write C, I don't use tags at all, and the language requires
a typedef equivalent. Here, I might occasionally use 'void*' for a
struct pointer when the struct-type involved is not visible.

Re: Why does C allow structs to have a tag?

<safcs6$a2t$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17048&group=comp.lang.c#17048

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 13:49:25 +0200
Organization: A noiseless patient Spider
Lines: 66
Message-ID: <safcs6$a2t$1@dont-email.me>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <87wnqsix3z.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 17 Jun 2021 11:49:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="84a9859ed97ba4c46a46185f09e499d2";
logging-data="10333"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX182WfUx9IuBaK9Wk1MbAPK0zGibdlZNNn4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:Yug8D7IBfvyF5uVpjNgQdco5V4w=
In-Reply-To: <87wnqsix3z.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Thu, 17 Jun 2021 11:49 UTC

On 17/06/2021 12:50, Ben Bacarisse wrote:
> Bart <bc@freeuk.com> writes:
>
>> On 17/06/2021 03:39, Andrey Tarasevich wrote:
>>> On 6/6/2021 5:16 AM, James Harris wrote:
>>>> Does C, as a language, need to allow its structs to have tags?
>>>>
>>>> So could C's struct tags be omitted from the language? If not, what does a struct tag add? Is it something to do with forward declarations
>>>> or syntactic consistency with union tags, etc?
>>> The tag is absolutely necessary when you want to declare a
>>> self-referential struct type, like a node in a linked list that must contain a pointer to the same type.
>>> Just try declaring a linked list node without using a tag. Report to the
>>> forum.
>>
>> It's not actually that hard, you just use void*, with the odd cast as needed:
>
>> typedef struct {
>> int data;
>> void* next;
>> } node;
>
>> When a cast is needed, you use (node*). No tags required. It's not as
>> type-safe, but then blame those stupid tags.
>
> The natural C definition is
>
> struct node {
> int data;
> struct node *next;
> };
>
> Which has served me well for decades. But, if you don't want to call
> the type by its natural name, just write
>
> typedef struct node node;
> struct node {
> int data;
> node *next;
> };
>
> and live happily ever after. Why would anyone use void * just to avoid
> a single use of a struct tag?
>

Or if the two declarations are bothersome:

typedef struct node {
int data;
struct node *next;
} node;

Or if the repeated "node" is troublesome :

#define Struct(N) typedef struct N N; struct N

Struct(node) {
int data;
node *next;
};

Of course, that won't work if your aim is to say how problematic C's
flexibility is. And it won't work if you have decided that macros
(except for the silly, confusing and useless "#define While for") are
not to be used.

Re: Why does C allow structs to have a tag?

<MnHyI.13116$IPne.12920@fx27.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17049&group=comp.lang.c#17049

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx27.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <87wnqsix3z.fsf@bsb.me.uk>
<safcs6$a2t$1@dont-email.me>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <safcs6$a2t$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210616-4, 16/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 108
Message-ID: <MnHyI.13116$IPne.12920@fx27.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Thu, 17 Jun 2021 12:39:08 UTC
Organization: virginmedia.com
Date: Thu, 17 Jun 2021 13:39:04 +0100
X-Received-Bytes: 3998
 by: Bart - Thu, 17 Jun 2021 12:39 UTC

On 17/06/2021 12:49, David Brown wrote:
> On 17/06/2021 12:50, Ben Bacarisse wrote:
>> Bart <bc@freeuk.com> writes:

>>> When a cast is needed, you use (node*). No tags required. It's not as
>>> type-safe, but then blame those stupid tags.
>>
>> The natural C definition is
>>
>> struct node {
>> int data;
>> struct node *next;
>> };
>>
>> Which has served me well for decades. But, if you don't want to call
>> the type by its natural name, just write
>>
>> typedef struct node node;
>> struct node {
>> int data;
>> node *next;
>> };
>>
>> and live happily ever after. Why would anyone use void * just to avoid
>> a single use of a struct tag?
>>
>
> Or if the two declarations are bothersome:
>
> typedef struct node {
> int data;
> struct node *next;
> } node;
>
>
> Or if the repeated "node" is troublesome :
>
> #define Struct(N) typedef struct N N; struct N
>
> Struct(node) {
> int data;
> node *next;
> };
>

This is good. With this macro, you're 1% along the path of creating a
new, improved language. Although not of course as good as a purpose-made
language, since it sits on top of C:

Your proposal still relies on struct tags. It creates 'node' in two
parallel namespaces. It allows both 'node X' and 'struct node Y'.

It's also not clear how well it works, if at all, with mutually
referential structs.

That is why such macro solutions don't really work. They also suffer
from the problem of everyone developing their own, incompatible workarounds.

> Of course, that won't work if your aim is to say how problematic C's
> flexibility is. And it won't work if you have decided that macros
> (except for the silly, confusing and useless "#define While for") are
> not to be used.

There was a discussion on Reddit about how the C-style loop:

for (A; B; C) D;

was not really equivalent to:

A;
while (B) {
D;
C;
}

because it had different behaviour with 'continue'; with 'for', it would
skip C, but with 'while', it would execute C.

I was saying most such loops, when not obviously performing iteration,
were really just while-loops and should be written as such.

To get around the problem with continue, I proposed a simple extension
to while-loops, acquiring one of the features of C for-loops, to make
them more attractive for such purposes. In my syntax (this wasn't a
specific C-language forum):

while B, C do
D
end

or:

while B do
D
step
C
end

I implemented both as proof-of-concept (and have retained the first as I
couldn't find a more suitable keyword for the second). In C syntax that
first might be:

while (B; C) D

Hence the reason I chose to use While to highlight that this is intended
as a while-loop, and not leave it to the reader to figure out what the
loop was supposed to do.

Re: Why does C allow structs to have a tag?

<safhbn$a29$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17050&group=comp.lang.c#17050

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 15:05:59 +0200
Organization: A noiseless patient Spider
Lines: 122
Message-ID: <safhbn$a29$1@dont-email.me>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <87wnqsix3z.fsf@bsb.me.uk>
<safcs6$a2t$1@dont-email.me> <MnHyI.13116$IPne.12920@fx27.ams4>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 17 Jun 2021 13:05:59 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="84a9859ed97ba4c46a46185f09e499d2";
logging-data="10313"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19s+drdFxfSwbwx1ZOkQ5/PndeDLrrpukQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:F5M2eBT1e7MvAmrM/AI0zYaFeHo=
In-Reply-To: <MnHyI.13116$IPne.12920@fx27.ams4>
Content-Language: en-GB
 by: David Brown - Thu, 17 Jun 2021 13:05 UTC

On 17/06/2021 14:39, Bart wrote:
> On 17/06/2021 12:49, David Brown wrote:
>> On 17/06/2021 12:50, Ben Bacarisse wrote:
>>> Bart <bc@freeuk.com> writes:
>
>>>> When a cast is needed, you use (node*). No tags required. It's not as
>>>> type-safe, but then blame those stupid tags.
>>>
>>> The natural C definition is
>>>
>>>    struct node {
>>>        int data;
>>>        struct node *next;
>>>    };
>>>
>>> Which has served me well for decades.  But, if you don't want to call
>>> the type by its natural name, just write
>>>
>>>    typedef struct node node;
>>>    struct node {
>>>         int data;
>>>         node *next;
>>>    };
>>>
>>> and live happily ever after.  Why would anyone use void * just to avoid
>>> a single use of a struct tag?
>>>
>>
>> Or if the two declarations are bothersome:
>>
>> typedef struct node {
>>     int data;
>>     struct node *next;
>> } node;
>>
>>
>> Or if the repeated "node" is troublesome :
>>
>> #define Struct(N) typedef struct N N; struct N
>>
>> Struct(node) {
>>      int data;
>>      node *next;
>> };
>>
>
> This is good. With this macro, you're 1% along the path of creating a
> new, improved language. Although not of course as good as a purpose-made
> language, since it sits on top of C:

I'm not interested in making a new language, and I don't see this syntax
as being either better or worse than that of C - such details are
irrelevant in practice. It's nothing more than showing how you can
declare recursive structures in C without having to repeat tag or type
names, if such repetition bothers you.

>
> Your proposal still relies on struct tags. It creates 'node' in two
> parallel namespaces. It allows both 'node X' and 'struct node Y'.

No one /cares/. This is not a problem - not in real life, for people
writing real code. It has been somewhat interesting to discuss why C is
the way it is here - history can often be interesting. But no one is
actually concerned about the practice, or thinks there are real
problems, or thinks there is scope for real improvement.

>
> It's also not clear how well it works, if at all, with mutually
> referential structs.
>
> That is why such macro solutions don't really work. They also suffer
> from the problem of everyone developing their own, incompatible
> workarounds.

Macros are fine for this kind of thing if you think it makes the code
clearer. They /do/ work. Of course unnecessary, gratuitous use will be
counter-productive - surely that goes without saying, as it can be
applied to /everything/ in programming, indeed, everything in life.

And I think every serious programmer will be far happier to deal with a
few odd and unnecessary C macros like this to work around "defects" in C
than an entire incompatible home-made language created to avoid such
imaginary defects in C.

>
>> Of course, that won't work if your aim is to say how problematic C's
>> flexibility is.  And it won't work if you have decided that macros
>> (except for the silly, confusing and useless "#define While for") are
>> not to be used.
>
> There was a discussion on Reddit about how the C-style loop:
>
>    for (A; B; C) D;
>
> was not really equivalent to:
>
>    A;
>    while (B) {
>       D;
>       C;
>    }
>
> because it had different behaviour with 'continue'; with 'for', it would
> skip C, but with 'while', it would execute C.

There are other differences.

>
> I was saying most such loops, when not obviously performing iteration,
> were really just while-loops and should be written as such.

If you want to write a "while" loop, C has a construct for that - it's
called "while".

It takes a special kind of perverse delight in causing confusion to
create a macro called "While" that has the meaning of "for" rather than
"while". Please do not do that unless it is in a thread about finding
confusing ways to write code - it just adds more work for others to try
to correct your misunderstandings and explain your mistakes and mixups
so that inexperienced C programmers don't confuse your posts with
reality or good advice.

Re: Why does C allow structs to have a tag?

<JbIyI.201853$co68.69985@fx03.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17051&group=comp.lang.c#17051

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx03.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <87wnqsix3z.fsf@bsb.me.uk>
<safcs6$a2t$1@dont-email.me> <MnHyI.13116$IPne.12920@fx27.ams4>
<safhbn$a29$1@dont-email.me>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <safhbn$a29$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210616-4, 16/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 85
Message-ID: <JbIyI.201853$co68.69985@fx03.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Thu, 17 Jun 2021 13:34:33 UTC
Organization: virginmedia.com
Date: Thu, 17 Jun 2021 14:34:29 +0100
X-Received-Bytes: 4230
 by: Bart - Thu, 17 Jun 2021 13:34 UTC

On 17/06/2021 14:05, David Brown wrote:
> On 17/06/2021 14:39, Bart wrote:

>> This is good. With this macro, you're 1% along the path of creating a
>> new, improved language. Although not of course as good as a purpose-made
>> language, since it sits on top of C:
>
> I'm not interested in making a new language,

Everytime you respond to a flaw or omission by finding a macro
workaround, then you are creating new language elements.

>

>> Your proposal still relies on struct tags. It creates 'node' in two
>> parallel namespaces. It allows both 'node X' and 'struct node Y'.
>
> No one /cares/.

Well, the OP cares. Purists might care: C only has tags now because they
were there to start with. A proper typedef could have done away with
them. Now they're like one of Charles' carbuncles.

This is not a problem - not in real life, for people
> writing real code. It has been somewhat interesting to discuss why C is
> the way it is here - history can often be interesting. But no one is
> actually concerned about the practice, or thinks there are real
> problems, or thinks there is scope for real improvement.
>
>>
>> It's also not clear how well it works, if at all, with mutually
>> referential structs.
>>
>> That is why such macro solutions don't really work. They also suffer
>> from the problem of everyone developing their own, incompatible
>> workarounds.
>
> Macros are fine for this kind of thing if you think it makes the code
> clearer. They /do/ work. Of course unnecessary, gratuitous use will be
> counter-productive - surely that goes without saying, as it can be
> applied to /everything/ in programming, indeed, everything in life.
>
> And I think every serious programmer will be far happier to deal with a
> few odd and unnecessary C macros like this to work around "defects" in C
> than an entire incompatible home-made language created to avoid such
> imaginary defects in C.

Here's how my own 'macro' works, here defining mutually referential structs:

record node=
int data
ref node2 nextnode
end

record node2=
real data
ref node nextnode
end

It can be 'preprocessed' to normal C code full of struct tags and
whatnot. But:

* It uses syntax in line with the rest of the language
* It doesn't need to drag in a macro definition
* It only allows 'node' etc to be used, not 'struct node'
* It doesn't create a parallel 'node' symbol in another namespace
* It works fine with circular or mutual references (these can be in
either order)

> If you want to write a "while" loop, C has a construct for that - it's
> called "while".

Which hardly anybody uses! Nearly all for-loops I see that aren't
clearly an iteration are really just while-loops.

Most of them don't use 'continue' either so the minor discrepancy I
mentioned is not relevant.

> It takes a special kind of perverse delight in causing confusion

.... by writing an obvious while loop as a for-loop. The tweak I
suggested can encourage more people to use the more appropriate kind of
loop.

Re: Why does C allow structs to have a tag?

<safpb1$5it$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17053&group=comp.lang.c#17053

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 08:22:07 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <safpb1$5it$1@dont-email.me>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 17 Jun 2021 15:22:09 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3018e23044534f428d83aa6ab57edcd4";
logging-data="5725"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/t+FCnxFAuKLs09HnJ6s8c"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.1
Cancel-Lock: sha1:iF1vBSLcNey9TFdtRNz9Pi1AAuo=
In-Reply-To: <duFyI.21896$R591.16204@fx37.ams4>
Content-Language: en-US
 by: Andrey Tarasevich - Thu, 17 Jun 2021 15:22 UTC

On 6/17/2021 3:29 AM, Bart wrote:
> On 17/06/2021 03:39, Andrey Tarasevich wrote:
>> On 6/6/2021 5:16 AM, James Harris wrote:
>>> Does C, as a language, need to allow its structs to have tags?
>>>
>>> So could C's struct tags be omitted from the language? If not, what
>>> does a struct tag add? Is it something to do with forward
>>> declarations or syntactic consistency with union tags, etc?
>>
>> The tag is absolutely necessary when you want to declare a
>> self-referential struct type, like a node in a linked list that must
>> contain a pointer to the same type.
>>
>> Just try declaring a linked list node without using a tag. Report to
>> the forum.
>>
>
> It's not actually that hard, you just use void*, with the odd cast as
> needed:

As I explicitly stated in the post you were responding to:

> P.S. Of course, you can get around this by using `void *` pointers
and explicit type casts everywhere, but that would still represent a
major loss of language functionality.

And that is basically the answer to the original question: C language
_needs_ struct tags to avoid having to "just use void*, with the odd
cast as needed". Having to do the latter would, of course, be an
abomination.

--
Best regards,
Andrey Tarasevich

Re: Why does C allow structs to have a tag?

<87r1h0iiiy.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17054&group=comp.lang.c#17054

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 17:05:09 +0100
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <87r1h0iiiy.fsf@bsb.me.uk>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <87wnqsix3z.fsf@bsb.me.uk>
<xZFyI.49758$aOx3.30095@fx22.ams4>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="dba4e90f2d9a52676a13a099c8050f76";
logging-data="19879"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nz/W9HTEZOZ0n8HtqQ4Coj8KzkHbx0L4="
Cancel-Lock: sha1:Axp+oefSvR41vPGxuWv//DPgydI=
sha1:13MGcbCS2O4BY/7oXiQ/ENZ8ipo=
X-BSB-Auth: 1.dab9beccab2f5d16166c.20210617170509BST.87r1h0iiiy.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 17 Jun 2021 16:05 UTC

Bart <bc@freeuk.com> writes:

> On 17/06/2021 11:50, Ben Bacarisse wrote:
>> Bart <bc@freeuk.com> writes:
>>
>>> On 17/06/2021 03:39, Andrey Tarasevich wrote:
>>>> On 6/6/2021 5:16 AM, James Harris wrote:
>>>>> Does C, as a language, need to allow its structs to have tags?
>>>>>
>>>>> So could C's struct tags be omitted from the language? If not, what does a struct tag add? Is it something to do with forward declarations
>>>>> or syntactic consistency with union tags, etc?
>>>> The tag is absolutely necessary when you want to declare a
>>>> self-referential struct type, like a node in a linked list that must contain a pointer to the same type.
>>>> Just try declaring a linked list node without using a tag. Report to the
>>>> forum.
>>>
>>> It's not actually that hard, you just use void*, with the odd cast as needed:
>>
>>> typedef struct {
>>> int data;
>>> void* next;
>>> } node;
>>
>>> When a cast is needed, you use (node*). No tags required. It's not as
>>> type-safe, but then blame those stupid tags.
>> The natural C definition is
>> struct node {
>> int data;
>> struct node *next;
>> };
>> Which has served me well for decades.
>
> I've used linked lists without needing the concept of a 'tag' for
> decades too.

But not in C, or do really use void * to avoid the canonical form?

>> But, if you don't want to call
>> the type by its natural name, just write
>> typedef struct node node;
>> struct node {
>> int data;
>> node *next;
>> };
>> and live happily ever after. Why would anyone use void * just to avoid
>> a single use of a struct tag?
>
> I was responding to the "The tag is absolutely necessary ... "
> comment.

I know. I just wanted to make sure no one took the void * idea
seriously by showing what everyone else does.

--
Ben.

Re: Why does C allow structs to have a tag?

<20210617090939.633@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17055&group=comp.lang.c#17055

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 563-365-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 16:22:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <20210617090939.633@kylheku.com>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <safpb1$5it$1@dont-email.me>
Injection-Date: Thu, 17 Jun 2021 16:22:59 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f629c8e6b16ac9acc3678bc5a54d887b";
logging-data="25838"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/a+ln7apDItXw2iWKaG154ZIJrDAA7pCg="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:9mTe5zHJQQLaZ0cCWRh/34crNgU=
 by: Kaz Kylheku - Thu, 17 Jun 2021 16:22 UTC

On 2021-06-17, Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
> On 6/17/2021 3:29 AM, Bart wrote:
>> On 17/06/2021 03:39, Andrey Tarasevich wrote:
>>> On 6/6/2021 5:16 AM, James Harris wrote:
>>>> Does C, as a language, need to allow its structs to have tags?
>>>>
>>>> So could C's struct tags be omitted from the language? If not, what
>>>> does a struct tag add? Is it something to do with forward
>>>> declarations or syntactic consistency with union tags, etc?
>>>
>>> The tag is absolutely necessary when you want to declare a
>>> self-referential struct type, like a node in a linked list that must
>>> contain a pointer to the same type.
>>>
>>> Just try declaring a linked list node without using a tag. Report to
>>> the forum.
>>>
>>
>> It's not actually that hard, you just use void*, with the odd cast as
>> needed:
>
> As I explicitly stated in the post you were responding to:
>
> > P.S. Of course, you can get around this by using `void *` pointers
> and explicit type casts everywhere, but that would still represent a
> major loss of language functionality.
>
> And that is basically the answer to the original question: C language
> _needs_ struct tags to avoid having to "just use void*, with the odd
> cast as needed". Having to do the latter would, of course, be an
> abomination.

But that's literally untrue true.

C programs need struct tags according to the design of the C language
we are stuck with. The language doesn't need it; the design is not
necessary, and even the C++ dialect shows that:

struct node {
node *next, *prev; // not a reference to the tag namespace
};

While that is a valid point, it is nonproductive and off-topic.

BartC likes to turn every discussion into some unproductive banter with
no useful outcome, about how C could be (or could have been) something else,
but without any concrete action plan to do anything about it.

It is unclear what outcome he's looking for, but judging by the history,
he seems satisfied with amusing himself by insulting comp.lang.c users
by insisting that they do not know any other language and cannot imagine
language design variations or features outside of the C design due
to the proverbial blinders on their eyes, in contrast to his amazingly
broad perspective gained from working on his special language projects
(which have no public repo, no documentation, no test cases, no users).

When you make careless comments like that the C language needs tags for
self-reference in declarations, you're just playing into that.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: Why does C allow structs to have a tag?

<saftl2$4s3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17056&group=comp.lang.c#17056

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 18:35:46 +0200
Organization: A noiseless patient Spider
Lines: 121
Message-ID: <saftl2$4s3$1@dont-email.me>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <87wnqsix3z.fsf@bsb.me.uk>
<safcs6$a2t$1@dont-email.me> <MnHyI.13116$IPne.12920@fx27.ams4>
<safhbn$a29$1@dont-email.me> <JbIyI.201853$co68.69985@fx03.ams4>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 17 Jun 2021 16:35:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="84a9859ed97ba4c46a46185f09e499d2";
logging-data="4995"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Tfv3N/GH1frmrCkiWbc1Ix0m5r8STqnw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:jk/+ZftaB6uuQSh//cRfDA6L+Dg=
In-Reply-To: <JbIyI.201853$co68.69985@fx03.ams4>
Content-Language: en-GB
 by: David Brown - Thu, 17 Jun 2021 16:35 UTC

On 17/06/2021 15:34, Bart wrote:
> On 17/06/2021 14:05, David Brown wrote:
>> On 17/06/2021 14:39, Bart wrote:
>
>>> This is good. With this macro, you're 1% along the path of creating a
>>> new, improved language. Although not of course as good as a purpose-made
>>> language, since it sits on top of C:
>>
>> I'm not interested in making a new language,
>
> Everytime you respond to a flaw or omission by finding a macro
> workaround, then you are creating new language elements.

First, I am not responding to a "flaw" or an "omission" - I am
responding to your desires (or at least your post) to make linked lists
without struct tags. I don't see any flaws or omissions in the C
language here - people have used the language to make linked lists
without any trouble.

Secondly, macros are part of the language - they are not making "new
language elements" any more than defining a function would be.

>
>>
>
>>> Your proposal still relies on struct tags. It creates 'node' in two
>>> parallel namespaces. It allows both 'node X' and 'struct node Y'.
>>
>> No one /cares/.
>
> Well, the OP cares. Purists might care: C only has tags now because they
> were there to start with. A proper typedef could have done away with
> them. Now they're like one of Charles' carbuncles.
>

If the language had been designed differently, it would have been
different. Well, now we know that.

As for "purists" - pure what?

And no, the OP does not care if C has struct tags or not - he was
curious as to whether they are necessary or not, and why the language
has them. That is completely different. (I am often curious about why
C has or does not have particular features, even though I might be quite
happy either way.)

>   This is not a problem - not in real life, for people
>> writing real code.  It has been somewhat interesting to discuss why C is
>> the way it is here - history can often be interesting.  But no one is
>> actually concerned about the practice, or thinks there are real
>> problems, or thinks there is scope for real improvement.
>>
>>>
>>> It's also not clear how well it works, if at all, with mutually
>>> referential structs.
>>>
>>> That is why such macro solutions don't really work. They also suffer
>>> from the problem of everyone developing their own, incompatible
>>> workarounds.
>>
>> Macros are fine for this kind of thing if you think it makes the code
>> clearer.  They /do/ work.  Of course unnecessary, gratuitous use will be
>> counter-productive - surely that goes without saying, as it can be
>> applied to /everything/ in programming, indeed, everything in life.
>>
>> And I think every serious programmer will be far happier to deal with a
>> few odd and unnecessary C macros like this to work around "defects" in C
>> than an entire incompatible home-made language created to avoid such
>> imaginary defects in C.
>
> Here's how my own 'macro' works, here defining mutually referential
> structs:
>
>     record node=
>         int data
>         ref node2 nextnode
>     end
>
>     record node2=
>         real data
>         ref node nextnode
>     end
>
> It can be 'preprocessed' to normal C code full of struct tags and
> whatnot. But:
>
> * It uses syntax in line with the rest of the language
> * It doesn't need to drag in a macro definition
> * It only allows 'node' etc to be used, not 'struct node'
> * It doesn't create a parallel 'node' symbol in another namespace
> * It works fine with circular or mutual references (these can be in
> either order)
>

Oh, that's just incredible! I imagine your language features here have
saved at least /minutes/ of your programming life, compared to writing
them in C!

>
>> If you want to write a "while" loop, C has a construct for that - it's
>> called "while".
>
> Which hardly anybody uses! Nearly all for-loops I see that aren't
> clearly an iteration are really just while-loops.

Your opinions as to what you think people use in C are not something I
consider realistic or valuable.

>
> Most of them don't use 'continue' either so the minor discrepancy I
> mentioned is not relevant.
>
>> It takes a special kind of perverse delight in causing confusion
>
> ... by writing an obvious while loop as a for-loop. The tweak I
> suggested can encourage more people to use the more appropriate kind of
> loop.
>

If it is a while loop, write "while". That's what C programmers do.

Re: Why does C allow structs to have a tag?

<U9LyI.145287$gpy1.37016@fx11.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17057&group=comp.lang.c#17057

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.dns-netz.com!news.freedyn.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx11.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <safpb1$5it$1@dont-email.me>
<20210617090939.633@kylheku.com>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <20210617090939.633@kylheku.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210617-8, 17/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 52
Message-ID: <U9LyI.145287$gpy1.37016@fx11.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Thu, 17 Jun 2021 16:57:24 UTC
Organization: virginmedia.com
Date: Thu, 17 Jun 2021 17:57:21 +0100
X-Received-Bytes: 3226
 by: Bart - Thu, 17 Jun 2021 16:57 UTC

On 17/06/2021 17:22, Kaz Kylheku wrote:
> On 2021-06-17, Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:

> BartC likes to turn every discussion into some unproductive banter with
> no useful outcome, about how C could be (or could have been) something else,
> but without any concrete action plan to do anything about it.

(That is not true. I've created several alternatives over approx 40
years, for in-house or personal use. Although they weren't really
alternatives for at least the first 15 years.

The problem is no one is interested in alternatives, and I don't think
anyone here has to the power to actually change the language anyway.
(Besides, with C being so firmly ensconced as part of Unix and Linux, it
isn't going anywhere.)

But what some people might have been able to do far better than me - 20+
years ago would have been better - is to have created an up-to-date
systems language of a similar ilk, something I would have respected a
lot more, that would have become mainstream, and without losing many of
the traits that are needed.

Instead we end up with ones like Rust and Zig that things harder rather
than easier. Zig (2019 version) even banished newlines from source code!
(CRLF newlines weren't allowed; they had to be LF only. CRLF have since
been allowed; but it still bans hard tabs.))

> It is unclear what outcome he's looking for, but judging by the history,
> he seems satisfied with amusing himself by insulting comp.lang.c users

I make a point of never insulting anybody online unless they get personal.

> by insisting that they do not know any other language and cannot imagine
> language design variations or features outside of the C design due
> to the proverbial blinders on their eyes, in contrast to his amazingly
> broad perspective gained from working on his special language projects
> (which have no public repo, no documentation, no test cases, no users).
>
> When you make careless comments like that the C language needs tags for
> self-reference in declarations, you're just playing into that.

This was the challenge:

AT:
> Just try declaring a linked list node without using a tag. Report to
> the forum.

I responded to it, and supplied example /C/ code.

Re: Why does C allow structs to have a tag?

<8qLyI.2052$xn1.1114@fx09.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17058&group=comp.lang.c#17058

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!fdc3.netnews.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx09.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <87wnqsix3z.fsf@bsb.me.uk>
<safcs6$a2t$1@dont-email.me> <MnHyI.13116$IPne.12920@fx27.ams4>
<safhbn$a29$1@dont-email.me> <JbIyI.201853$co68.69985@fx03.ams4>
<saftl2$4s3$1@dont-email.me>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <saftl2$4s3$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210617-8, 17/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 21
Message-ID: <8qLyI.2052$xn1.1114@fx09.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Thu, 17 Jun 2021 17:14:44 UTC
Organization: virginmedia.com
Date: Thu, 17 Jun 2021 18:14:40 +0100
X-Received-Bytes: 1842
 by: Bart - Thu, 17 Jun 2021 17:14 UTC

On 17/06/2021 17:35, David Brown wrote:
> On 17/06/2021 15:34, Bart wrote:

>> Which hardly anybody uses! Nearly all for-loops I see that aren't
>> clearly an iteration are really just while-loops.
>
> Your opinions as to what you think people use in C are not something I
> consider realistic or valuable.

It isn't what I think, it's what I see.

Not everyone writes code like David Brown. Or me.

> If it is a while loop, write "while". That's what C programmers do.

If only! Every time I look aat for-loop in other people's code, I have
to analyse it to figure exactly what it is. Here's a case in point:

for (n = num, len = 0; (! len && ! n) || n > 0; len++) {

Re: Why does C allow structs to have a tag?

<sag0s6$mta$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17059&group=comp.lang.c#17059

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!vHj1Qq4NDvrye2NzbaJyCQ.user.gioia.aioe.org.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 19:30:43 +0200
Organization: Aioe.org NNTP Server
Lines: 45
Message-ID: <sag0s6$mta$1@gioia.aioe.org>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <safpb1$5it$1@dont-email.me>
NNTP-Posting-Host: vHj1Qq4NDvrye2NzbaJyCQ.user.gioia.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Complaints-To: abuse@aioe.org
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Thu, 17 Jun 2021 17:30 UTC

Le 17/06/2021 à 17:22, Andrey Tarasevich a écrit :
> On 6/17/2021 3:29 AM, Bart wrote:
>> On 17/06/2021 03:39, Andrey Tarasevich wrote:
>>> On 6/6/2021 5:16 AM, James Harris wrote:
>>>> Does C, as a language, need to allow its structs to have tags?
>>>>
>>>> So could C's struct tags be omitted from the language? If not, what
>>>> does a struct tag add? Is it something to do with forward
>>>> declarations or syntactic consistency with union tags, etc?
>>>
>>> The tag is absolutely necessary when you want to declare a
>>> self-referential struct type, like a node in a linked list that must
>>> contain a pointer to the same type.
>>>
>>> Just try declaring a linked list node without using a tag. Report to
>>> the forum.
>>>
>>
>> It's not actually that hard, you just use void*, with the odd cast as
>> needed:
>
> As I explicitly stated in the post you were responding to:
>
> > P.S. Of course, you can get around this by using `void *` pointers
> and explicit type casts everywhere, but that would still represent a
> major loss of language functionality.
>
> And that is basically the answer to the original question: C language
> _needs_ struct tags to avoid having to "just use void*, with the odd
> cast as needed". Having to do the latter would, of course, be an
> abomination.

Yup. Using void * can be powerful but bypasses type checking, and not
only that, but you also lose the self-documentation a proper type provides.

Regarding casts, they are usually not needed though if you always assign
the void * pointer (for instance here, the 'next' member) to a pointer
to the struct, and conversely.

So if your pointer to node is 'Node', for instance, something like 'Node
= Node->next' is perfectly valid without any cast. Or 'Node2->next = Node1'.

Casts would be needed if you directly want to access a member from the
'next' pointer, but there are lots of ways to avoid that, so that point
is relatively moot.

Re: Why does C allow structs to have a tag?

<20210617103159.394@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17060&group=comp.lang.c#17060

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 563-365-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 17 Jun 2021 17:53:17 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 81
Message-ID: <20210617103159.394@kylheku.com>
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <safpb1$5it$1@dont-email.me>
<20210617090939.633@kylheku.com> <U9LyI.145287$gpy1.37016@fx11.ams4>
Injection-Date: Thu, 17 Jun 2021 17:53:17 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f629c8e6b16ac9acc3678bc5a54d887b";
logging-data="4501"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ucvYPYvrCNKtL2JXmDj4WirmPQOmaqQ0="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:Kl8X6zNzKVouYl2RR/iXSYq63Zs=
 by: Kaz Kylheku - Thu, 17 Jun 2021 17:53 UTC

On 2021-06-17, Bart <bc@freeuk.com> wrote:
> On 17/06/2021 17:22, Kaz Kylheku wrote:
>> On 2021-06-17, Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
>
>
>> BartC likes to turn every discussion into some unproductive banter with
>> no useful outcome, about how C could be (or could have been) something else,
>> but without any concrete action plan to do anything about it.
>
> (That is not true. I've created several alternatives over approx 40
> years, for in-house or personal use. Although they weren't really
> alternatives for at least the first 15 years.

That is not doing anything the problem of what C is, from the
perspective of people who are stuck using it, who come to the newsgroup
to discuss about how to use it.

> The problem is no one is interested in alternatives, and I don't think

No, that isn't the problem. People are absolutely keenly interested in
alternatives and using them. Anyone who is using Rust, or C++, or Go
is probably interested in C alternatives.

Some people using C are also interested in C alternatives, but
for whatever pragmatic reasons are using C anyway.

People don't go to a comp.lang.c newsgroup to discuss alternatives
though; we know where to find alternatives and get help.

> anyone here has to the power to actually change the language anyway.

If you don't seize the power, you will never have the power.

I believe I have the power to change C; if I really wanted to badly
enough, I could avail myself of that influence.

I would form a plan, identify the roadblocks in it and work on removing
them one by one.

Changing C is a procedure, a task. Every task has a finite number of
steps. First you have to figure out what they are. You might start with
the wrong ones. Once you are on the right track though, it's just a
matter of taking the first step, then the second and so on.

(If the change idea is unreasonable, though, there will likely be an
impassable roadblock. Changes are generally expected to be thoroughly
backward compatible, so that the vast majority existing programs compile
and have an unaltered behavior, and the remainder are only in some
dubious bucket we are willing to throw under the bus.)

> (Besides, with C being so firmly ensconced as part of Unix and Linux, it
> isn't going anywhere.)

That is false; the bleeding edge of C is changing, and the GNU C
Compiler Collection project is actively maintained, cranking out new
releases.

> But what some people might have been able to do far better than me - 20+
> years ago would have been better - is to have created an up-to-date

20 years ago isn't coming back though.

> Instead we end up with ones like Rust and Zig that things harder rather
> than easier.

These projects have their reasons though. They are real things in the
real world.

You can download them from a repo, build them, and run a test suite.

You can join a mailing list, web forum or IRC channel or whatever and
meet others users.

If you don't think Rust and Zig are the answer, the only way is to
compete with your own, join those projects to steer their direction, or
else forever be complaining that people are making all these various C
replacements, but not doing it how you would like.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: Why does C allow structs to have a tag?

<0fOyI.205832$TPRa.75795@fx01.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17061&group=comp.lang.c#17061

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx01.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <safpb1$5it$1@dont-email.me>
<20210617090939.633@kylheku.com> <U9LyI.145287$gpy1.37016@fx11.ams4>
<20210617103159.394@kylheku.com>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <20210617103159.394@kylheku.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Antivirus: AVG (VPS 210617-8, 17/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 46
Message-ID: <0fOyI.205832$TPRa.75795@fx01.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Thu, 17 Jun 2021 20:27:40 UTC
Organization: virginmedia.com
Date: Thu, 17 Jun 2021 21:27:36 +0100
X-Received-Bytes: 2792
 by: Bart - Thu, 17 Jun 2021 20:27 UTC

On 17/06/2021 18:53, Kaz Kylheku wrote:
> On 2021-06-17, Bart <bc@freeuk.com> wrote:

[C alternatives]

> If you don't think Rust and Zig are the answer, the only way is to
> compete with your own, join those projects to steer their direction, or
> else forever be complaining that people are making all these various C
> replacements, but not doing it how you would like.

It isn't really for me. I'm stuck with using my languages for the same
reason I'd be stuck working as self-employed, if I was still working.

But I constantly see the problems people have with C, not here much,
usenet is pretty much dead, but in places like reddit and others, a lot
of things which would otherwise have been a piece of cake if the
language had done things properly.

I just really feel sorry for them, as I can't help.

Many of these are small things that do not warrant switching to a
totally new and very different language (which would introduce 100 new
problems).

But let's take one example of something posted here, someone wanted to
print this:

printf("UINT64_MAX : %lld\n", UINT64_MAX);

C has made it fashionable to make this stuff much harder than it need be
(Zig is worse than C for printing). The full solution for this was (see
stdint.h thread):

#include <stdio.h> # needed for printf
#include <stdint.h> # needed for UINT64_MAX and for inttypes.h

printf("UINT64_MAX : " PRId64 "\n", UINT64_MAX);

(It will be something alone those lines; I'm not going to bother
checking it.)

Basically, they needed to print a number to the console, something that
in the 1970s would have been utterly trivial:

print x

Re: Why does C allow structs to have a tag?

<5de1487f-9750-4fd4-9cda-eb0cee427dc8n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17062&group=comp.lang.c#17062

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:240b:: with SMTP id fv11mr2771542qvb.23.1623977887437;
Thu, 17 Jun 2021 17:58:07 -0700 (PDT)
X-Received: by 2002:a05:620a:1113:: with SMTP id o19mr6692603qkk.229.1623977887309;
Thu, 17 Jun 2021 17:58:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 17 Jun 2021 17:58:07 -0700 (PDT)
In-Reply-To: <0fOyI.205832$TPRa.75795@fx01.ams4>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.45; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.45
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <safpb1$5it$1@dont-email.me>
<20210617090939.633@kylheku.com> <U9LyI.145287$gpy1.37016@fx11.ams4>
<20210617103159.394@kylheku.com> <0fOyI.205832$TPRa.75795@fx01.ams4>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5de1487f-9750-4fd4-9cda-eb0cee427dc8n@googlegroups.com>
Subject: Re: Why does C allow structs to have a tag?
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Fri, 18 Jun 2021 00:58:07 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3647
 by: Öö Tiib - Fri, 18 Jun 2021 00:58 UTC

On Thursday, 17 June 2021 at 23:27:54 UTC+3, Bart wrote:
> On 17/06/2021 18:53, Kaz Kylheku wrote:
> > On 2021-06-17, Bart <b...@freeuk.com> wrote:
>
> [C alternatives]
> > If you don't think Rust and Zig are the answer, the only way is to
> > compete with your own, join those projects to steer their direction, or
> > else forever be complaining that people are making all these various C
> > replacements, but not doing it how you would like.
> It isn't really for me. I'm stuck with using my languages for the same
> reason I'd be stuck working as self-employed, if I was still working.
>
> But I constantly see the problems people have with C, not here much,
> usenet is pretty much dead, but in places like reddit and others, a lot
> of things which would otherwise have been a piece of cake if the
> language had done things properly.
>
> I just really feel sorry for them, as I can't help.
>
> Many of these are small things that do not warrant switching to a
> totally new and very different language (which would introduce 100 new
> problems).
>
> But let's take one example of something posted here, someone wanted to
> print this:
>
> printf("UINT64_MAX : %lld\n", UINT64_MAX);
>
> C has made it fashionable to make this stuff much harder than it need be
> (Zig is worse than C for printing). The full solution for this was (see
> stdint.h thread):
>
> #include <stdio.h> # needed for printf
> #include <stdint.h> # needed for UINT64_MAX and for inttypes.h
>
> printf("UINT64_MAX : " PRId64 "\n", UINT64_MAX);
>
> (It will be something alone those lines; I'm not going to bother
> checking it.)

You did not use %:

printf("%" PRId64 "\n", UINT64_MAX);

Yes such not overly trivial way, but trivial enough.

> Basically, they needed to print a number to the console, something that
> in the 1970s would have been utterly trivial:
>
> print x

In the 1970s what is currently called 'UINT64_MAX' was utterly trivially
called 'x'? I think you misremember.

The trouble only starts when you output UINT64_MAX as you will see that
all these excels and libreoffices to where someone might want to paste
it fail to process it. They fail gracefully and silently by showing wrong
results. So you will need more C and more C.

Re: Why does C allow structs to have a tag?

<yw_yI.212250$TPRa.93209@fx01.ams4>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17063&group=comp.lang.c#17063

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!fdc3.netnews.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx01.ams4.POSTED!not-for-mail
Subject: Re: Why does C allow structs to have a tag?
Newsgroups: comp.lang.c
References: <s9iea5$n5c$1@dont-email.me> <saeclh$e0n$1@dont-email.me>
<duFyI.21896$R591.16204@fx37.ams4> <safpb1$5it$1@dont-email.me>
<20210617090939.633@kylheku.com> <U9LyI.145287$gpy1.37016@fx11.ams4>
<20210617103159.394@kylheku.com> <0fOyI.205832$TPRa.75795@fx01.ams4>
<5de1487f-9750-4fd4-9cda-eb0cee427dc8n@googlegroups.com>
From: bc...@freeuk.com (Bart)
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
MIME-Version: 1.0
In-Reply-To: <5de1487f-9750-4fd4-9cda-eb0cee427dc8n@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 8bit
X-Antivirus: AVG (VPS 210618-0, 18/06/2021), Outbound message
X-Antivirus-Status: Clean
Lines: 83
Message-ID: <yw_yI.212250$TPRa.93209@fx01.ams4>
X-Complaints-To: http://netreport.virginmedia.com
NNTP-Posting-Date: Fri, 18 Jun 2021 10:25:34 UTC
Organization: virginmedia.com
Date: Fri, 18 Jun 2021 11:25:27 +0100
X-Received-Bytes: 3845
 by: Bart - Fri, 18 Jun 2021 10:25 UTC

On 18/06/2021 01:58, Öö Tiib wrote:
> On Thursday, 17 June 2021 at 23:27:54 UTC+3, Bart wrote:

>> But let's take one example of something posted here, someone wanted to
>> print this:
>>
>> printf("UINT64_MAX : %lld\n", UINT64_MAX);
>>
>> C has made it fashionable to make this stuff much harder than it need be
>> (Zig is worse than C for printing). The full solution for this was (see
>> stdint.h thread):
>>
>> #include <stdio.h> # needed for printf
>> #include <stdint.h> # needed for UINT64_MAX and for inttypes.h
>>
>> printf("UINT64_MAX : " PRId64 "\n", UINT64_MAX);
>>
>> (It will be something alone those lines; I'm not going to bother
>> checking it.)
>
> You did not use %:
>
> printf("%" PRId64 "\n", UINT64_MAX);
>
> Yes such not overly trivial way, but trivial enough.

You helped make my point for me. What a palaver to just print a number!
And so easy to get wrong.

>> Basically, they needed to print a number to the console, something that
>> in the 1970s would have been utterly trivial:
>>
>> print x
>
> In the 1970s what is currently called 'UINT64_MAX' was utterly trivially
> called 'x'? I think you misremember.

UINT64_MAX is just a number. That is not the main issue here. Given the
existence of such a value, you should be able to do:

print UINT64_MAX

plus whatever extra syntax the language requires. #includes, inttype.h,
"%" and "PRId64" is not mere syntax.

To get on to the MAX part, If I do this in my language, it is just:

println =u64.max

..max can be applied to any integer type or expression. The "=" adds the
label that is present in the C. The output is:

MAX(U64)= 18446744073709551615

> The trouble only starts when you output UINT64_MAX as you will see that
> all these excels and libreoffices to where someone might want to paste
> it fail to process it. They fail gracefully and silently by showing wrong
> results. So you will need more C and more C.

How is this relevant? A language shouldn't allow you to just write:

print(a)

because some extreme values of a, IF you were to paste them elsewhere,
might cause problems with buggy software? How would adding more C fix that?

When I paste this value, it is just a string anyway. BTW here's the next
one up:

println =u128.max

It shows:

MAX(U128)= 340282366920938463463374607431768211455

A u128 type is merely double the word size of the current crop of 64-bit
machines. A systems language should deal with this stuff effortlessly.

If I paste /that/ value into Google, I get a list of 128-bit resources.

Re: Why does C allow structs to have a tag?

<867dijb22a.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17073&group=comp.lang.c#17073

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 24 Jun 2021 10:33:01 -0700
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <867dijb22a.fsf@linuxsc.com>
References: <s9iea5$n5c$1@dont-email.me> <s9oidl$te9$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="5f9785b3afd09fb507884e20b2dfc5bb";
logging-data="11437"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OPAIGgOmt97cJduYghVoeoGareclo8r4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:5hUtBIVh2x8RljFsr13eqqrb4PM=
sha1:plxOjYSL44eCSM18ssE0KGIXQy0=
 by: Tim Rentsch - Thu, 24 Jun 2021 17:33 UTC

John Bode <jfbode1029@gmail.com> writes:

[...]

> To reiterate a point I make a lot - typedef on its own creates leaky
> abstractions. If I need to know that I have to use the `.` or `->`
> operator on something, I'd rather have it declared as `struct A foo;` or
> `struct B *ptr;` instead of a type name that doesn't convey struct-ness
> at all. Same reason I don't like it when people hide pointers behind
> typedefs - I once spent half a day chasing my tail because somebody
> created a typedef name for a pointer type and used that as a template
> parameter for a vector in C++, such that when I was using an iterator
> I needed to write
>
> (*it)->do_something();
>
> However, since the typedef name didn't indicate pointer-ness *at all*, I
> wound up writing
>
> it->do_something();
>
> and g++ vomited up hundreds of incomprehensible error messages that
> basically boiled down to "you need to use a * here, dummy".

Sounds to me like the culprit is C++, not typedefs.

Re: Why does C allow structs to have a tag?

<2c251905-2ee0-44f0-bac6-2233d23bfcb1n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17075&group=comp.lang.c#17075

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:75c3:: with SMTP id z3mr6500367qtq.308.1624567556913;
Thu, 24 Jun 2021 13:45:56 -0700 (PDT)
X-Received: by 2002:ad4:56e5:: with SMTP id cr5mr7106304qvb.7.1624567556763;
Thu, 24 Jun 2021 13:45:56 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Thu, 24 Jun 2021 13:45:56 -0700 (PDT)
In-Reply-To: <867dijb22a.fsf@linuxsc.com>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.45; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.45
References: <s9iea5$n5c$1@dont-email.me> <s9oidl$te9$1@dont-email.me> <867dijb22a.fsf@linuxsc.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2c251905-2ee0-44f0-bac6-2233d23bfcb1n@googlegroups.com>
Subject: Re: Why does C allow structs to have a tag?
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Thu, 24 Jun 2021 20:45:56 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Öö Tiib - Thu, 24 Jun 2021 20:45 UTC

On Thursday, 24 June 2021 at 20:33:12 UTC+3, Tim Rentsch wrote:
> John Bode <jfbod...@gmail.com> writes:
>
> [...]
>
> > To reiterate a point I make a lot - typedef on its own creates leaky
> > abstractions. If I need to know that I have to use the `.` or `->`
> > operator on something, I'd rather have it declared as `struct A foo;` or
> > `struct B *ptr;` instead of a type name that doesn't convey struct-ness
> > at all. Same reason I don't like it when people hide pointers behind
> > typedefs - I once spent half a day chasing my tail because somebody
> > created a typedef name for a pointer type and used that as a template
> > parameter for a vector in C++, such that when I was using an iterator
> > I needed to write
> >
> > (*it)->do_something();
> >
> > However, since the typedef name didn't indicate pointer-ness *at all*, I
> > wound up writing
> >
> > it->do_something();
> >
> > and g++ vomited up hundreds of incomprehensible error messages that
> > basically boiled down to "you need to use a * here, dummy".
>
> Sounds to me like the culprit is C++, not typedefs.

It is good old issue of three star programmers (that both C and C++
share) and on given case it was additionally obfuscated by using typedef
of pointer. By most C coding guidelines I've seen it is advised not to
typedef pointers that are meant to be dereferenced.

Re: Why does C allow structs to have a tag?

<sblh9g$muc$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17164&group=comp.lang.c#17164

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jfbode1...@gmail.com (John Bode)
Newsgroups: comp.lang.c
Subject: Re: Why does C allow structs to have a tag?
Date: Thu, 1 Jul 2021 17:57:52 -0500
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <sblh9g$muc$1@dont-email.me>
References: <s9iea5$n5c$1@dont-email.me> <s9oidl$te9$1@dont-email.me>
<867dijb22a.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 1 Jul 2021 22:57:52 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="eaa0d35b3a64df86461f8a55fb3f1b2c";
logging-data="23500"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xTPBQyVhYZGZhFEhv3RyK"
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.11.0
Cancel-Lock: sha1:/yDd9Khc7T7lqws0S+agzmJXSR8=
In-Reply-To: <867dijb22a.fsf@linuxsc.com>
Content-Language: en-US
 by: John Bode - Thu, 1 Jul 2021 22:57 UTC

On 6/24/21 12:33 PM, Tim Rentsch wrote:
> John Bode <jfbode1029@gmail.com> writes:
>
> [...]
>
>> To reiterate a point I make a lot - typedef on its own creates leaky
>> abstractions. If I need to know that I have to use the `.` or `->`
>> operator on something, I'd rather have it declared as `struct A foo;` or
>> `struct B *ptr;` instead of a type name that doesn't convey struct-ness
>> at all. Same reason I don't like it when people hide pointers behind
>> typedefs - I once spent half a day chasing my tail because somebody
>> created a typedef name for a pointer type and used that as a template
>> parameter for a vector in C++, such that when I was using an iterator
>> I needed to write
>>
>> (*it)->do_something();
>>
>> However, since the typedef name didn't indicate pointer-ness *at all*, I
>> wound up writing
>>
>> it->do_something();
>>
>> and g++ vomited up hundreds of incomprehensible error messages that
>> basically boiled down to "you need to use a * here, dummy".
>
> Sounds to me like the culprit is C++, not typedefs.
>

g++ doesn't handle errors in template parameters very well and generates
a *lot* of hard-to-follow error messages for relatively simple mistakes.
And at the time I was still relatively inexperienced with C++, which
didn't help.

But the typedef name (or, more properly, the incomplete and leaky
abstraction introduced by that typedef name) was the actual culprit.
Again, there was nothing to tell me that I was iterating over a vector
of *pointers*, not a vector of instances, which required me to use
different syntax.

With better error messages I would have figured it out in a few minutes
rather than half a day, but it was still time lost to a leaky
abstraction.

That was an egregious case, but far from the only one. Hiding pointers
and struct types behind typedefs without an API to handle pointer and
member selection operations for you is unambiguously bad practice, and
people need to stop doing it.

Pages:12345678910111213141516
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor