Re: struct enumerating types




"santosh" <santosh.k83@xxxxxxxxx> wrote in message
news:1169884861.545415.101460@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Lane Straatman wrote:
"Lane Straatman" <invalid@xxxxxxxxxxx> wrote in message

I would like to write a 'struct'. I have a library that is all but
completely inappropriate for this task.

Huh? I can't quite understand what you're trying to say?
Thanks for your reply. The books on my shelf are woefully inadequate to
shepherd me through type definitions in C99.

So I'm looking for C code that fills in the gaps between:
#undef whateverkeithsaysfor99compliance
#define whateverkeithsays for99compliance
Mr. Thompson wonders whether I was trying to get his attention with this.
On the one hand, I was trying to be brief in pseudocode. I think that in
order to write this program in a minimally portable fashion, one needs to
have some preprocessor instructions. CBfalconer had these on a related
question; however it was one that was slightly less general than the current
one:
/* Standard defines of operators, usable on C90 up */
#ifndef stdops_h
#define stdops_h
#if defined(__STDC__) && (__STDC_VERSION__ >= 199901L)
/* The following from C99 - must define for C90 */
#include <stdbool.h> /* define bool, true, false */
#include <iso646.h> /* define not, and, or */
#else
#define false 0
#define true 1
typedef int bool;
#define not !
#define and &&
#define or ||
#define xor ^
#endif
#endif
Mr. Thompson added the #undef's:
#if __STDC_VERSION__ >= 199901L
#include <stdbool.h>
#else
#undef false
#undef true
#undef bool
#define false 0
#define true 1
#define bool int
#endif
, which seemed like something prudential. I probably was trying to get his
attention, not like throwing a brick through the window, but like flagging
something for perusal. I wouldn't have an executable right now without his
help.

and another dimension with the appropriate printf specifier, e.g.
foo.int.format = "%i";
foo.char.format = "%c";

Do you mean you want some tool to automatically generate struct
declarations and printf() format specifiers for them? Why would you
want that?
I'd like to write a program that creates a variable of every type I'm likely
to meet in C99, and prints it out. So when I want to know the the
conversion argument for a pointer to an unsigned char, I can re-use the
source.

foo.a = 40;
foo.b = 'k';

You can't assign to types anymore than you can assign to int. No memory
is allocated for them. You need to declare one or more objects of type
foo and assign values to them.
I totally fanned on a correct C struct declaration. When I started looking
at ISO C, my windows, C and C++ were all roughly in the same place in my
head. So you start fencing off what is kosher and what is an extension. I
didn't create an object because I thought an object looked like this:

//////////////////////////////////////////////////////////
// The one and only CTja39App object

CTja39App theApp;

//////////////////////////////////////
, which is a leftover from the way MFC instantiates. I'm not the only
person who misunderstands the way the word "object" operates in C.

#ifdef _WIN32
# define FORMAT_LONG_LONG "I64"
#else
# define FORMAT_LONG_LONG "ll"
#endif

#include <stdio.h>
int main(void)
{
struct foo {
int a;
char b;
signed char c;
unsigned char d;
short e;
unsigned short f;
unsigned int g;
long h;
unsigned long i;
__int64 j;
unsigned __int64 k;
}; struct foo foo_obj;

foo_obj.a = 40;
foo_obj.b = 'k';
foo_obj.c = 'k';
foo_obj.d = 'k';
foo_obj.e = 123;
foo_obj.f = 300;
foo_obj.g = 50000;
foo_obj.h = 123123123;
foo_obj.i = 123123123;
foo_obj.j = 123451234512345;
foo_obj.k = 123451234512345;


printf("%i\n", foo_obj.a);
printf("%c\n", foo_obj.b);
printf("%c\n", foo_obj.c);
printf("%uc\n", foo_obj.d);
printf("%hi\n", foo_obj.e);
printf("%hc\n", foo_obj.f);
printf("%ui\n", foo_obj.g);
printf("%li\n", foo_obj.h);
printf("%lu\n", foo_obj.i);
printf("%" FORMAT_LONG_LONG "d\n", foo_obj.j );
printf("%" FORMAT_LONG_LONG "d\n", foo_obj.k );


return 0;
}
Q2) Are the format specifiers right? Obviously my version of long long is
non-standard. The unsigned char conversion seems wrong.

Q3) How would I extend foo to have the conversion characters associated
with their type? If I had a field of ten characters, I could capture
expressions like '"%lu"'. This wouldn't help me with print statements like
that for type of size_t:
printf("%lu", (unsigned long)sizeof(thetype));
, but one problem at a time.
LS




.



Relevant Pages