Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > size of an array without sizeof operator

Reply
Thread Tools

size of an array without sizeof operator

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      03-05-2007
Is it possible to measure the size of an array without using the
sizeof operator ?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-05-2007
http://www.velocityreviews.com/forums/(E-Mail Removed), India wrote:
> Is it possible to measure the size of an array without using the
> sizeof operator ?
>

Why would you want to? That's what sizeof is for.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-05-2007
(E-Mail Removed), India wrote:
> Is it possible to measure the size of an array without using the
> sizeof operator ?


Not portably. sizeof is the standard for a reason, why do you want to
jump through hoops to simulate it imperfectly?

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      03-05-2007
"(E-Mail Removed), India" <(E-Mail Removed)>
writes:
> Is it possible to measure the size of an array without using the
> sizeof operator ?


The answer is the same as it has been in every single thread
that brings up this stupid question. Do some research.
--
"Am I missing something?"
--Dan Pop
 
Reply With Quote
 
William Ahern
Guest
Posts: n/a
 
      03-05-2007
On Mon, 05 Mar 2007 14:28:20 +1300, Ian Collins wrote:

> (E-Mail Removed), India wrote:
>> Is it possible to measure the size of an array without using the
>> sizeof operator ?
>>

> Why would you want to? That's what sizeof is for.
>


To please your professor?

I was going to say "To pass a class". But that would be rather ambiguous
 
Reply With Quote
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      03-05-2007
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.

 
Reply With Quote
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      03-05-2007
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.



 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-05-2007
(E-Mail Removed), India wrote, On 05/03/07 01:47:
> I saw this question in one of the earlier posts but the answer was not
> given.


<snip>

How can you have seen the question but not seen the answer that it
always given?

Also, please quote just enough of the post you are replying to to
provide context. There is no guarantee that the person reading your post
will have seen (or will ever see) the post you are replying to.
--
Flash Gordon
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-05-2007
"(E-Mail Removed), India" wrote:
>

.... snip ...
>
> Suppose I have, for some specific Type,
> Type a[size];
>
> I calculate
>
> sprintf(str1, "%p", (void *)(a+1));
> sprintf(str2, "%p", (void *)a);
>

.... snip ...
>
> Correct me for all errors.


Why should the result of the sprintf be meaningful as an integer?
Maybe the output of sprintf is "Go to schoolhouse, room C. Select
small boy third from left in front row. Ask him where that data is
stored. Give him a slate on which to write his answer."

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      03-05-2007
<(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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[MUDFLAP] Is sizeof(ARRAY[0]) equivalent to sizeof(*ARRAY) ? m.labanowicz@gmail.com C Programming 46 01-14-2013 10:50 PM
The sizeof operator : sizeof(++i) Kislay C Programming 10 10-19-2007 09:28 PM
How to calculate size of an int without using the sizeof operator but using bitwise operator Manish_Ganvir C Programming 13 02-14-2005 07:24 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments