ManicQin said:
I would ignore most of the comments
but I do want to reply to somethings
1) sizeof a pointer is 4 (platform specific)
2)I did not know if offsetof is language standart or vc6 so I added it
3) this is not homework!! It is a real live crapy system that
generates reciepts that I was given to support and fix up a bit... As
for today every time we need to add a parameter to our structs I need
to add his offset and sizeof to a different array (it's not even an
array it a function with switch cases!!! yack!)
I'm just trying to fix up the system . I would prefer to delete it and
start over but hey it is not my call.
I am not a c programmer but that's why I ask questions here (after i
googled them)!
I cannot rely on people not messing with my struct so i cannot know
what is after or before the requested var thanks NOOB
and by the way the alignment is dealed thanks REH
Sorry, your question looked very much like a homework assignment, and
we do get a lot of students showing up with "Please do my homework for
me" questions.
I'll take your word for it that it's a real system (though I *hope*
that it doesn't really declare a structure "ThePimp" with members
"rings" and "blings").
If you're not a C programmer, I wonder why you were assigned the task
of working on C software. On the other hand, I suppose you're a C
programmer now, but you might be in over your head. But that's
between you and whoever gave you this task.
Getting back to the point, here's the code I was hesitant to post
before:
#include <stdio.h>
int main(void)
{
struct ThePimp {
char rings[10];
char blings[20];
};
const size_t blings_size = sizeof ((struct ThePimp*)0)->blings;
printf("blings_size = %lu\n", (unsigned long)blings_size);
return 0;
}
This is how you can determine the size of a member of a struct
(blings in this case) given only the struct type, not an object
of the type. It looks ugly, but I'm reasonably sure it's safe
and portable.
"(struct ThePimp*)0" yields a null pointer of type "pointer to struct
ThePimp". A null pointer is a unique and valid pointer value that
you can't legally dereference, since it doesn't point to anything.
"((struct ThePimp*)0)->blings" dereferences the pointer and yields
the value of the "blings" member of the object that it points to.
(Yes, I know, I just said that you can't dererence it and that it
doesn't point to anything; bear with me.)
"sizeof ((struct ThePimp*)0)->blings" yields the size of the "blings"
member of this nonexistent structure. This actually works because
the "sizeof" operator doesn't evaluate its operand; all it does
is determine its operand's type and give you its size in bytes.
(There's an exception for VLAs (variable-length arrays), but that
doesn't apply here.) We needed an expression of the type of the
"blings" member, but since we only wanted to apply "sizeof" to it,
we didn't need an expression that can actually be evaluated.
(size_t is the unsigned integer type of the result of the sizeof
operator. I converted it to unsigned long so I could print it with
the "%lu" format.)
I *think* that's what you were asking for. But I would suggest
that you consider carefully whether it's really what you need.
The expression is rather obscure, and getting the size of "blings"
is going to be much easier if you have an object of type "struct
ThePimp". I would expect that, in most contexts where you need
the size of a member of a struct, you're just naturally going
to have an object of the struct type lying around, so you just
do the much simpler "sizeof struct_object.blings", or perhaps
"sizeof pointer_object->blings".
But if I really needed to use this, I'd wrap it in a macro (for one
thing, it lets me apply a name to the construct so it's easier to tell
at a glance what it's intended to do). Here's a revised version of
the program:
#include <stdio.h>
#define MEMBER_SIZE(s, m) (sizeof ((s*)0)->m)
int main(void)
{
struct ThePimp {
char rings[10];
char blings[20];
};
printf("blings size = %lu\n",
(unsigned long)MEMBER_SIZE(struct ThePimp, blings));
return 0;
}
The best advice I can give you is to read the comp.lang.c FAQ,
<
http://www.c-faq.com/>. I also recommend picking up a copy of K&R2
(Kernighan & Ritchie, "The C Programming Language", 2nd edition).