Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #149463 > unrolled thread

is there a way to ask a variable what it is?

Started byT <T@invalid.invalid>
First post2020-01-28 19:19 -0800
Last post2020-02-19 12:23 +0100
Articles 14 on this page of 54 — 21 participants

Back to article view | Back to comp.lang.c


Contents

  is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 19:19 -0800
    Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-29 03:47 +0000
      Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:20 -0800
        Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-29 19:18 +0000
          Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 20:58 +0000
            Re: is there a way to ask a variable what it is? nobody@example.org (Scott) - 2020-01-30 15:59 +0000
              Re: is there a way to ask a variable what it is? scott@slp53.sl.home (Scott Lurndal) - 2020-01-30 16:35 +0000
      Re: is there a way to ask a variable what it is? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-28 21:55 -0800
        Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-30 00:24 +0000
          solved: Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 21:51 -0800
            Re: solved: Re: is there a way to ask a variable what it is? fir <profesor.fir@gmail.com> - 2020-01-30 03:16 -0800
    Re: is there a way to ask a variable what it is? Ian Collins <ian-news@hotmail.com> - 2020-01-29 17:28 +1300
      Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:55 -0800
        Re: is there a way to ask a variable what it is? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-29 06:49 +0000
        Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 08:08 +0100
        Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-30 00:14 +0000
          solved: Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 21:51 -0800
          Re: is there a way to ask a variable what it is? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-18 08:56 -0800
            Re: is there a way to ask a variable what it is? already5chosen@yahoo.com - 2020-02-19 01:19 -0800
              Re: is there a way to ask a variable what it is? chad <cdalten@gmail.com> - 2020-02-19 02:53 -0800
              Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-19 12:45 +0000
              Re: is there a way to ask a variable what it is? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-19 07:55 -0500
              Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-02-19 13:02 +0000
                Re: is there a way to ask a variable what it is? chad <cdalten@gmail.com> - 2020-02-19 07:27 -0800
              Re: is there a way to ask a variable what it is? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-22 09:42 -0800
    Re: is there a way to ask a variable what it is? Robert Wessel <robertwessel2@yahoo.com> - 2020-01-28 22:36 -0600
      Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:55 -0800
        Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 13:22 +0100
          Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-29 13:57 +0100
            Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 17:47 +0100
              Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-30 12:06 +0100
      Re: is there a way to ask a variable what it is? scott@slp53.sl.home (Scott Lurndal) - 2020-01-29 15:10 +0000
        Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 17:07 +0100
    Re: is there a way to ask a variable what it is? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-28 21:52 -0800
    Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 11:11 +0000
      Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 13:30 +0100
        Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 12:44 +0000
          Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 14:03 +0100
            Re: is there a way to ask a variable what it is? gazelle@shell.xmission.com (Kenny McCormack) - 2020-01-29 14:03 +0000
            Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 14:30 +0000
              Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 15:45 +0100
                Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 16:28 +0100
                  Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 08:35 -0800
                  Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 17:34 +0000
                    Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 18:48 +0100
      Re: is there a way to ask a variable what it is? Thiago Adams <thiago.adams@gmail.com> - 2020-01-29 04:43 -0800
        Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-29 14:46 +0100
          Re: is there a way to ask a variable what it is? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-29 15:15 +0000
            Re: is there a way to ask a variable what it is? fir <profesor.fir@gmail.com> - 2020-01-29 08:11 -0800
            Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 08:38 -0800
    Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 11:23 +0000
    Re: is there a way to ask a variable what it is? John Bode <jfbode1029@gmail.com> - 2020-01-29 11:55 -0800
    Re: is there a way to ask a variable what it is? jacobnavia <jacob@jacob.remcomp.fr> - 2020-02-19 12:23 +0100
    Re: is there a way to ask a variable what it is? jacobnavia <jacob@jacob.remcomp.fr> - 2020-02-19 12:23 +0100

Page 3 of 3 — ← Prev page 1 2 [3]


#149485

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-29 15:45 +0100
Message-ID<r0s5pm$hfa$1@news.albasani.net>
In reply to#149484
> I tried:
> 
>      int A; float B; int C; long long int D;
>      int (*E)[];
> 
>      std::cout<<typeid(A+B).name() << std::endl;
>      std::cout<<typeid(A+C).name() << std::endl;
>      std::cout<<typeid(A+D).name() << std::endl;
>      std::cout<<typeid(E).name() << std::endl;
> 
> (you don't need the intermediate variable.) It prints:
> 
>      f
>      i
>      x
>      PA_i;

I tried this (MSVC):

#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
	int A, C;
	float B;
	long long D;
	int (*E)[];
	cout << typeid(A + B).name() << endl;
	cout << typeid(A + C).name() << endl;
	cout << typeid(A + D).name() << endl;
	cout << typeid(E).name()     << endl;
}

It prints:
float
int
__int64
int (* __ptr64)[0]

Don't know why to turn on more expressiveness for g++.

[toc] | [prev] | [next] | [standalone]


#149488

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-29 16:28 +0100
Message-ID<r0s8ar$bls$1@news.albasani.net>
In reply to#149485
> I tried this (MSVC):
> 
> #include <iostream>
> #include <typeinfo>
> 
> using namespace std;
> 
> int main()
> {
>      int A, C;
>      float B;
>      long long D;
>      int (*E)[];
>      cout << typeid(A + B).name() << endl;
>      cout << typeid(A + C).name() << endl;
>      cout << typeid(A + D).name() << endl;
>      cout << typeid(E).name()     << endl;
> }
> 
> It prints:
> float
> int
> __int64
> int (* __ptr64)[0]
> 
> Don't know why to turn on more expressiveness for g++.

That's a proper solutution:

#include <iostream>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{
	unordered_map<type_index, string> typeMappings;
	typeMappings[type_index( typeid(char) )]               = "char";
	typeMappings[type_index( typeid(unsigned char) )]      = "unsigned char";
	typeMappings[type_index( typeid(signed char) )]        = "signed char";
	typeMappings[type_index( typeid(short) )]              = "short";
	typeMappings[type_index( typeid(unsigned short) )]     = "unsigned short";
	typeMappings[type_index( typeid(int) )]                = "int";
	typeMappings[type_index( typeid(unsigned int) )]       = "unsigned int";
	typeMappings[type_index( typeid(long) )]               = "long";
	typeMappings[type_index( typeid(unsigned long) )]      = "unsigned long";
	typeMappings[type_index( typeid(long long) )]          = "long long";
	typeMappings[type_index( typeid(unsigned long long) )] = "unsigned long 
long";
	typeMappings[type_index( typeid(float) )]              = "float";
	typeMappings[type_index( typeid(double) )]             = "double";
	typeMappings[type_index( typeid(long double) )]        = "long double";

	int a, c;
	float b;
	long long d;
	cout << typeMappings[type_index( typeid(a + b) )] << endl;
	cout << typeMappings[type_index( typeid(a + c) )] << endl;
	cout << typeMappings[type_index( typeid(a + d) )] << endl;
}

It's just a little more to write, but technically that's more elegant.

[toc] | [prev] | [next] | [standalone]


#149493

FromT <T@invalid.invalid>
Date2020-01-29 08:35 -0800
Message-ID<r0sc8k$pnn$1@dont-email.me>
In reply to#149488
On 2020-01-29 07:28, Bonita Montero wrote:
>> I tried this (MSVC):
>>
>> #include <iostream>
>> #include <typeinfo>
>>
>> using namespace std;
>>
>> int main()
>> {
>>      int A, C;
>>      float B;
>>      long long D;
>>      int (*E)[];
>>      cout << typeid(A + B).name() << endl;
>>      cout << typeid(A + C).name() << endl;
>>      cout << typeid(A + D).name() << endl;
>>      cout << typeid(E).name()     << endl;
>> }
>>
>> It prints:
>> float
>> int
>> __int64
>> int (* __ptr64)[0]
>>
>> Don't know why to turn on more expressiveness for g++.
> 
> That's a proper solutution:
> 
> #include <iostream>
> #include <typeindex>
> #include <typeinfo>
> #include <unordered_map>
> #include <string>
> 
> using namespace std;
> 
> int main()
> {
>      unordered_map<type_index, string> typeMappings;
>      typeMappings[type_index( typeid(char) )]               = "char";
>      typeMappings[type_index( typeid(unsigned char) )]      = "unsigned 
> char";
>      typeMappings[type_index( typeid(signed char) )]        = "signed 
> char";
>      typeMappings[type_index( typeid(short) )]              = "short";
>      typeMappings[type_index( typeid(unsigned short) )]     = "unsigned 
> short";
>      typeMappings[type_index( typeid(int) )]                = "int";
>      typeMappings[type_index( typeid(unsigned int) )]       = "unsigned 
> int";
>      typeMappings[type_index( typeid(long) )]               = "long";
>      typeMappings[type_index( typeid(unsigned long) )]      = "unsigned 
> long";
>      typeMappings[type_index( typeid(long long) )]          = "long long";
>      typeMappings[type_index( typeid(unsigned long long) )] = "unsigned 
> long long";
>      typeMappings[type_index( typeid(float) )]              = "float";
>      typeMappings[type_index( typeid(double) )]             = "double";
>      typeMappings[type_index( typeid(long double) )]        = "long 
> double";
> 
>      int a, c;
>      float b;
>      long long d;
>      cout << typeMappings[type_index( typeid(a + b) )] << endl;
>      cout << typeMappings[type_index( typeid(a + c) )] << endl;
>      cout << typeMappings[type_index( typeid(a + d) )] << endl;
> }
> 
> It's just a little more to write, but technically that's more elegant.
> 

I am going to have a lot of fun with this!

[toc] | [prev] | [next] | [standalone]


#149499

FromBart <bc@freeuk.com>
Date2020-01-29 17:34 +0000
Message-ID<RmjYF.251861$_R5.219556@fx19.am4>
In reply to#149488
On 29/01/2020 15:28, Bonita Montero wrote:
>> I tried this (MSVC):
>>
>> #include <iostream>
>> #include <typeinfo>
>>
>> using namespace std;
>>
>> int main()
>> {
>>      int A, C;
>>      float B;
>>      long long D;
>>      int (*E)[];
>>      cout << typeid(A + B).name() << endl;
>>      cout << typeid(A + C).name() << endl;
>>      cout << typeid(A + D).name() << endl;
>>      cout << typeid(E).name()     << endl;
>> }
>>
>> It prints:
>> float
>> int
>> __int64
>> int (* __ptr64)[0]
>>
>> Don't know why to turn on more expressiveness for g++.
> 
> That's a proper solutution:
> 
> #include <iostream>
> #include <typeindex>
> #include <typeinfo>
> #include <unordered_map>
> #include <string>
> 
> using namespace std;
> 
> int main()
> {
>      unordered_map<type_index, string> typeMappings;
>      typeMappings[type_index( typeid(char) )]               = "char";
>      typeMappings[type_index( typeid(unsigned char) )]      = "unsigned 
> char";
>      typeMappings[type_index( typeid(signed char) )]        = "signed 
> char";
>      typeMappings[type_index( typeid(short) )]              = "short";
>      typeMappings[type_index( typeid(unsigned short) )]     = "unsigned 
> short";
>      typeMappings[type_index( typeid(int) )]                = "int";
>      typeMappings[type_index( typeid(unsigned int) )]       = "unsigned 
> int";
>      typeMappings[type_index( typeid(long) )]               = "long";
>      typeMappings[type_index( typeid(unsigned long) )]      = "unsigned 
> long";
>      typeMappings[type_index( typeid(long long) )]          = "long long";
>      typeMappings[type_index( typeid(unsigned long long) )] = "unsigned 
> long long";
>      typeMappings[type_index( typeid(float) )]              = "float";
>      typeMappings[type_index( typeid(double) )]             = "double";
>      typeMappings[type_index( typeid(long double) )]        = "long 
> double";
> 
>      int a, c;
>      float b;
>      long long d;
>      cout << typeMappings[type_index( typeid(a + b) )] << endl;
>      cout << typeMappings[type_index( typeid(a + c) )] << endl;
>      cout << typeMappings[type_index( typeid(a + d) )] << endl;
> }
> 
> It's just a little more to write, but technically that's more elegant.

This starts to look remarkably similar to C's _Generic approach. But it 
doesn't handle arbitrary types constructed from arrays and pointers.

Assuming the result of .name is a unique representation of the type, 
then a function can parse that into more helpful syntax, and for any 
type. So "PA_i" might become "pointer to array of int" or actual 
"int(*)[]" C syntax.



[toc] | [prev] | [next] | [standalone]


#149500

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-29 18:48 +0100
Message-ID<r0sgi0$m6q$1@news.albasani.net>
In reply to#149499
> This starts to look remarkably similar to C's _Generic approach. But
> it doesn't handle arbitrary types constructed from arrays and pointers.

The C-mess with macros doesn't either.
The whole thing should be useful for rare debugging-purposes only,
but theres no simple way either in C or in C++. But I think the C++
-way is more elegant.

[toc] | [prev] | [next] | [standalone]


#149478

FromThiago Adams <thiago.adams@gmail.com>
Date2020-01-29 04:43 -0800
Message-ID<a82c6f75-ec44-4c5a-8b40-e4a534e156d9@googlegroups.com>
In reply to#149474
On Wednesday, January 29, 2020 at 8:11:47 AM UTC-3, Bart wrote:
> On 29/01/2020 03:19, T wrote:
> > Hi All,
> > 
> > Trivia question:
> > 
> > In C, after you declare a variable, is there a way
> > to turn around and ask it what that variable is?
> > 
> > Some thing like Raku's
> > 
> >      > my uint $u; say $u.^name
> >      Int
> > 
> > only correct?
> > 
> > Many thanks,
> > -T
> > 
> 
> Only in a limited way, eg: (macro copied from stackoverflow article):
> 
> #include <stdio.h>
> #include <stddef.h>
> #include <stdint.h>
> 
> #define typename(x) _Generic((x),\
>    _Bool: "_Bool",\
>    unsigned char: "unsigned char",          \
>    char: "char",\
>    signed char: "signed char",            \
>    short int: "short int",\
>    unsigned short int: "unsigned short int",     \
>    int: "int",\
>    unsigned int: "unsigned int",           \
>    long int: "long int",\
>    unsigned long int: "unsigned long int",      \
>    long long int: "long long int",\
>    unsigned long long int: "unsigned long long int", \
>    float: "float",\
>    double: "double",                 \
>    long double: "long double",\
>    char *: "pointer to char",        \
>    void *: "pointer to void",\
>    int *: "pointer to int",         \
>   default: "other")
> 
> int main(void) {
>    int a;
>    float b;
>    char* c;
>    int d[10];
> 
>    printf("a is %s\n",typename(a));
>    printf("b is %s\n",typename(b));
>    printf("c is %s\n",typename(c));
>    printf("d is %s\n",typename(d));
> }

If you want to know the type of variable to create a declaration,
then gcc have typeof. 

https://gcc.gnu.org/onlinedocs/gcc/Typeof.html


[toc] | [prev] | [next] | [standalone]


#149482

FromDavid Brown <david.brown@hesbynett.no>
Date2020-01-29 14:46 +0100
Message-ID<r0s2bh$rae$1@dont-email.me>
In reply to#149478
On 29/01/2020 13:43, Thiago Adams wrote:
> On Wednesday, January 29, 2020 at 8:11:47 AM UTC-3, Bart wrote:
>> On 29/01/2020 03:19, T wrote:
>>> Hi All,
>>>
>>> Trivia question:
>>>
>>> In C, after you declare a variable, is there a way
>>> to turn around and ask it what that variable is?
>>>
>>> Some thing like Raku's
>>>
>>>      > my uint $u; say $u.^name
>>>      Int
>>>
>>> only correct?
>>>
>>> Many thanks,
>>> -T
>>>
>>
>> Only in a limited way, eg: (macro copied from stackoverflow article):
>>
>> #include <stdio.h>
>> #include <stddef.h>
>> #include <stdint.h>
>>
>> #define typename(x) _Generic((x),\
>>    _Bool: "_Bool",\
>>    unsigned char: "unsigned char",          \
>>    char: "char",\
>>    signed char: "signed char",            \
>>    short int: "short int",\
>>    unsigned short int: "unsigned short int",     \
>>    int: "int",\
>>    unsigned int: "unsigned int",           \
>>    long int: "long int",\
>>    unsigned long int: "unsigned long int",      \
>>    long long int: "long long int",\
>>    unsigned long long int: "unsigned long long int", \
>>    float: "float",\
>>    double: "double",                 \
>>    long double: "long double",\
>>    char *: "pointer to char",        \
>>    void *: "pointer to void",\
>>    int *: "pointer to int",         \
>>   default: "other")
>>
>> int main(void) {
>>    int a;
>>    float b;
>>    char* c;
>>    int d[10];
>>
>>    printf("a is %s\n",typename(a));
>>    printf("b is %s\n",typename(b));
>>    printf("c is %s\n",typename(c));
>>    printf("d is %s\n",typename(d));
>> }
> 
> If you want to know the type of variable to create a declaration,
> then gcc have typeof. 
> 
> https://gcc.gnu.org/onlinedocs/gcc/Typeof.html
> 

It depends on what you want to do with the type.  Often, you want to
declare a new variable of the same type - and for that, typeof is ideal.
 It is much like "decltype" in C++, though I don't know if it is always
exactly the same.  Combined with another gcc extension, statement
expressions, you can make macros like:

#define max(a,b) \
  ({ typeof (a) _a = (a); \
      typeof (b) _b = (b); \
    _a > _b ? _a : _b; })

These are useful extensions, and something that I think would be nice to
have added to the C standards.  (Perhaps there are good reasons for not
doing so.)  They let you write safer generic macros.

But "typeof" won't help the OP - it is not an expression whose value can
be used to print out a type name.  Bart's _Generic macro is, but it
won't handle more general types (structs, unions, other pointers, etc.)
without explicit extension.


[toc] | [prev] | [next] | [standalone]


#149487

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2020-01-29 15:15 +0000
Message-ID<slrnr338cs.km3.grahn+nntp@frailea.sa.invalid>
In reply to#149482
On Wed, 2020-01-29, David Brown wrote:
...
> But "typeof" won't help the OP
...

It's not clear that the OP /needs/ any help; it appeared to
be an idle question prompted by reading about reflection in
some other language.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#149491

Fromfir <profesor.fir@gmail.com>
Date2020-01-29 08:11 -0800
Message-ID<d38ee58f-dae5-4676-bb80-6a31932e0f8e@googlegroups.com>
In reply to#149487
W dniu środa, 29 stycznia 2020 16:15:49 UTC+1 użytkownik Jorgen Grahn napisał:
> On Wed, 2020-01-29, David Brown wrote:
> ...
> > But "typeof" won't help the OP
> ...
> 
> It's not clear that the OP /needs/ any help; it appeared to
> be an idle question prompted by reading about reflection in
> some other language.
> 

ye and this rises a question if thios question is good or annoying

(it could be annoying becouse basic question repeated by someone who dont want to learn only engage people to work at him, where this work resulrs will be abandoned at all, may be annoying)

let me answer this:

as i already said before i think this question is possibly ok but some simply should take care not to overuse it..this is a quantity question

[toc] | [prev] | [next] | [standalone]


#149494

FromT <T@invalid.invalid>
Date2020-01-29 08:38 -0800
Message-ID<r0sceo$qpt$1@dont-email.me>
In reply to#149487
On 2020-01-29 07:15, Jorgen Grahn wrote:
> On Wed, 2020-01-29, David Brown wrote:
> ...
>> But "typeof" won't help the OP
> ...
> 
> It's not clear that the OP /needs/ any help; it appeared to
> be an idle question prompted by reading about reflection in
> some other language.
> 
> /Jorgen
> 

I though it was clear that I did not need help when
I stated that it was a trivia question.  And yes,
it is an idle question, but not just of another language,
but one written in C, so C constraints do appear
from time to time.

[toc] | [prev] | [next] | [standalone]


#149475

FromBart <bc@freeuk.com>
Date2020-01-29 11:23 +0000
Message-ID<7XdYF.187570$UPz1.56842@fx15.am4>
In reply to#149463
On 29/01/2020 03:19, T wrote:
> Hi All,
> 
> Trivia question:
> 
> In C, after you declare a variable, is there a way
> to turn around and ask it what that variable is?
> 
> Some thing like Raku's
> 
>      > my uint $u; say $u.^name
>      Int

BTW that doesn't look right.

First, you are declaring $u to be uint, so you already know what type it 
is. But it's showing the type as Int which might be different from uint.

(C is always statically typed, so you know types of variables in the 
same way. It is in expressions where the macro I showed is more useful, 
as the result may not be obvious. Or possibly inside a macro that can be 
invoked wityh variables of different types.)

[toc] | [prev] | [next] | [standalone]


#149507

FromJohn Bode <jfbode1029@gmail.com>
Date2020-01-29 11:55 -0800
Message-ID<dd11bb2a-2661-4624-8198-aba56762a7c0@googlegroups.com>
In reply to#149463
On Tuesday, January 28, 2020 at 9:19:46 PM UTC-6, T wrote:
> Hi All,
> 
> Trivia question:
> 
> In C, after you declare a variable, is there a way
> to turn around and ask it what that variable is?
> 
> Some thing like Raku's
> 
>      > my uint $u; say $u.^name
>      Int
> 
> only correct?
> 
> Many thanks,
> -T

Nope.  C does not support any kind of runtime type information or reflection.  There's no
metadata stored with any object to indicate its type or size.  

[toc] | [prev] | [next] | [standalone]


#150078

Fromjacobnavia <jacob@jacob.remcomp.fr>
Date2020-02-19 12:23 +0100
Message-ID<1b29d293-93ae-4e14-ab0b-894c9107f200@jacob.remcomp.fr>
In reply to#149463
Le 29/01/2020 à 04:19, T a écrit :
> Hi All,
> 
> Trivia question:
> 
> In C, after you declare a variable, is there a way
> to turn around and ask it what that variable is?
> 
> Some thing like Raku's
> 
>      > my uint $u; say $u.^name
>      Int
> 
> only correct?
> 
> Many thanks,
> -T
> 

There are two ways to achieve that. One is very difficult and 
complicated, the other is easy and used in C by many people.

1) Complicated method: Use the debug information. The compiler will 
generate information describing ALL the types of the program and the 
name, address and all details of each variable in the program if you 
compile it with debug information (-g in many systems). There are APIs 
available sometimes to read the debug information and in that way you 
can always say what the type and value of any variable is. But this is 
the same amount of work as writing a debugger almost, so...

2) Use a union and write the type of the following data in the first 
part of the union. For instance:

typedef struct tagTypedData {
	int type;
	union data {
		char c;
		short s;
		int i;
		long l;
		void *p;
		long long ll;
		float f;
		double d;
		long double ld;
	} d;
} TypedData;

Then, you write:
#define CHAR 1
#define SHORT 2
... etc

When you create this struct, you write in the program

TypedData *newTypedData(int type, union data u)
{
	TypedData *result = malloc(sizeof(TypedData));
	result->type = type;
	result->d = data;
	return result;
}

This is doing run time type information manually, and is easy but boring...

jacob

[toc] | [prev] | [next] | [standalone]


#150079

Fromjacobnavia <jacob@jacob.remcomp.fr>
Date2020-02-19 12:23 +0100
Message-ID<r2j5rj$j00$2@dont-email.me>
In reply to#149463
Le 29/01/2020 à 04:19, T a écrit :
> Hi All,
> 
> Trivia question:
> 
> In C, after you declare a variable, is there a way
> to turn around and ask it what that variable is?
> 
> Some thing like Raku's
> 
>      > my uint $u; say $u.^name
>      Int
> 
> only correct?
> 
> Many thanks,
> -T
> 

There are two ways to achieve that. One is very difficult and 
complicated, the other is easy and used in C by many people.

1) Complicated method: Use the debug information. The compiler will 
generate information describing ALL the types of the program and the 
name, address and all details of each variable in the program if you 
compile it with debug information (-g in many systems). There are APIs 
available sometimes to read the debug information and in that way you 
can always say what the type and value of any variable is. But this is 
the same amount of work as writing a debugger almost, so...

2) Use a union and write the type of the following data in the first 
part of the union. For instance:

typedef struct tagTypedData {
	int type;
	union data {
		char c;
		short s;
		int i;
		long l;
		void *p;
		long long ll;
		float f;
		double d;
		long double ld;
	} d;
} TypedData;

Then, you write:
#define CHAR 1
#define SHORT 2
... etc

When you create this struct, you write in the program

TypedData *newTypedData(int type, union data u)
{
	TypedData *result = malloc(sizeof(TypedData));
	result->type = type;
	result->d = data;
	return result;
}

This is doing run time type information manually, and is easy but boring...

jacob

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c


csiph-web