Deutsch   English   Français   Italiano  
<87cylrwkdf.fsf@bsb.me.uk>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Ben Bacarisse <ben@bsb.me.uk>
Newsgroups: comp.lang.c
Subject: Re: how to make it work
Date: Thu, 29 Aug 2024 14:04:28 +0100
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <87cylrwkdf.fsf@bsb.me.uk>
References: <877194bd8a95b9acb03db317ee000e94cce2834d@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Thu, 29 Aug 2024 15:04:28 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="fea14ca971f37fc10e006db08518a19f";
	logging-data="4170496"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19ktJtbaRpOfggiox10615mscu4r4uTFOo="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:X3uZVC/YtUGplxUb7ki/8dVCc8w=
	sha1:4sKATPBcgUokiclq0ZW/sjgw8C0=
X-BSB-Auth: 1.8f60b7a1fef97d15e345.20240829140428BST.87cylrwkdf.fsf@bsb.me.uk
Bytes: 2845

fir <fir@grunge.pl> writes:

> see such code
>
>
> long long unsigned tag ;
>
> void foo(long long unsigned tag)
> {
>  if(tag=='warsaw') printf("\nwarsaw");
>  if(tag=='paris')  printf("\nparis");
>  if(tag=='new york') printf("\nnew york");
>  if(tag=='old york') printf("\nold york");
>  if(tag=='very old york') printf("\nvery old york");
>
>
> }
>
> int main(void)
> {
>  foo('warsaw');
>  foo('paris');
>  foo('new york');
>  foo('old york');
>  foo('very old york');
>
>  return 'bye';
> }
>
> and maybe guess the result (or how it should be)
> (later i may tell you)
>
> the problem is how to make it work i want to use that kind of
> 'tags' and would like to use it assuming at least 8 characters
> work okay i mean the code above would "catch" only on proper tag and
> each one would be printed one time
>
> right now it dont - is thsi a way to make it work
> (maybe except the last one as i understand
>  if(tag=='old york') printf("\nold york");
>  if(tag=='very old york') printf("\nvery old york");
> may be treated as the same
>
> (i need it to work on 32 bit old mingw/gcc)

That's unfortunate because this can be done in a portable and relatively
convenient way in modern C:

#include <stdint.h>
#include <stdio.h>

typedef union {
     unsigned char bytes[sizeof (uint64_t)];
     uint64_t tag;
} Tag;

void foo(Tag t)
{
     if (t.tag == (Tag){"warsaw"}.tag)
          printf("warsaw\n");
}

int main(void)
{
     foo((Tag){"warsaw"});
}

With a little bit more work, you can get this to work in older C without
compound literals.

But depending on what your ultimate goal is, you might want to look at
how Lisp implementations "intern" their symbols.  That can avoid the
obvious length limitations while making for very efficient equality
comparisons.

-- 
Ben.