<(E-Mail Removed)> wrote in message

news:(E-Mail Removed) oups.com...

>I saw this question in one of the earlier posts but the answer was not

> given.

>

> I am learning C from home. I have a PC at home. I am reading

> K & R second edition. I post my doubts in this forum to get clarified.

> I am preparing for interview. That is why I am asking.

>

> I thought of doing it in one way without using sizeof. But it

> ASSUMES that sizeof pointer should not be bigger than maximum

> value of unsigned long or unsigned long long. Before writing the

> code, let me explain it in plain English because, in this forum I wil

> get to know if the assumption is correct.

>

> In the following, assume that size, str, etc exist.

>

> Suppose I have, for some specific Type,

> Type a[size];

>

> I calculate

>

> sprintf(str1, "%p", (void *)(a+1));

> sprintf(str2, "%p", (void *)a);

>

> Then I have my own routine to convert str1 and str2 to unsigned

> long or unsigned long long based on the ASSUMPTION mentioned

> above. I find the difference of these converted numbers and then

> multiply the difference by size.

>

> This is what strikes me.

>

> Correct me for all errors.
"(char*)(a+1)-(char*)(a)" will yield the same result as sizeof(Type).

Multiply by "size" and you will get the size of the total array. No need to

jump through any further hoops like you describe.

Usually when people ask this question, they don't know "size", or they're

trying to reimplement the sizeof() function in general. Neither of those

problems can be solved portably; what you're asking is a more specific case

that can.

S

--

Stephen Sprunk "Those people who think they know everything

CCIE #3723 are a great annoyance to those of us who do."

K5SSS --Isaac Asimov

--

Posted via a free Usenet account from

http://www.teranews.com