Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

how to measure size of an array


Can anyone tell how to measure the size of an array without use of
sizeof operator ?

"asit dhal" <penasit@gmail.com> writes:
> Can anyone tell how to measure the size of an array without use of
> sizeof operator ?

How about referring to one of the many threads in the last few
months that discuss this same lame question, instead of starting
a new thread?
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
On Feb 14, 9:02 am, "asit dhal" <penasit@gmail.com> wrote:

> Can anyone tell how to measure the size of an array without use of
> sizeof operator ?

The size information is given at declaration of an array. When an
array is passed into a function, the sizeof can't evaluate the size of
the array inside the function.
asit dhal:

> Can anyone tell how to measure the size of an array without use of
> sizeof operator ?

    (char*)pover - (char*)p

--
~/JB\~

"asit dhal" <penasit@gmail.com> writes:
> Can anyone tell how to measure the size of an array without use of
> sizeof operator ?

Use the sizeof operator.  That's what it's for.

--
Keith Thompson (The_Other_Keith) k@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.

asit dhal wrote:
> Can anyone tell how to measure the size of an array without use of
> sizeof operator ?

No.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
                     --- Albert Einstein ---

lovecreatesbea@gmail.com said:

> On Feb 14, 9:02 am, "asit dhal" <penasit@gmail.com> wrote:
>> Can anyone tell how to measure the size of an array without use of
>> sizeof operator ?

> The size information is given at declaration of an array. When an
> array is passed into a function, the sizeof can't evaluate the size of
> the array inside the function.

You don't pass an array to a function. You pass a pointer to that
array's first element.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

"Jamie Boy" <a@b.c> wrote in message n
>> Can anyone tell how to measure the size of an array without use of
>> sizeof operator ?

>    (char*)pover - (char*)p

Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.

"Malcolm McLean" <regniz@btinternet.com> writes:
> "Jamie Boy" <a@b.c> wrote in message n
>>> Can anyone tell how to measure the size of an array without use of
>>> sizeof operator ?
>>    (char*)pover - (char*)p
> Make that unsigned char *.
> char * should be reserved for genuine character data, and can contain trap
> representations. A highly unlikely but allowable implementation could even
> crash your program if one of the pointers points to a trap representation.

I don't believe this.  Can you cite chapter & verse on that?
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Malcolm McLean wrote, On 14/02/07 22:50:

> "Jamie Boy" <a@b.c> wrote in message n
>>> Can anyone tell how to measure the size of an array without use of
>>> sizeof operator ?

>>    (char*)pover - (char*)p

> Make that unsigned char *.
> char * should be reserved for genuine character data, and can contain trap
> representations. A highly unlikely but allowable implementation could even
> crash your program if one of the pointers points to a trap representation.

What the pointers point to is irrelevant since they are not dereferenced
so casting to char* is fine. The problem with it is how you determine
the correct value of pover before you know the size of the array.
--
Flash Gordon

"Malcolm McLean" <regniz@btinternet.com> writes:
> "Jamie Boy" <a@b.c> wrote in message n
>>> Can anyone tell how to measure the size of an array without use of
>>> sizeof operator ?

>>    (char*)pover - (char*)p

> Make that unsigned char *.
> char * should be reserved for genuine character data, and can contain trap
> representations. A highly unlikely but allowable implementation could even
> crash your program if one of the pointers points to a trap representation.

I don't believe so.  The code does not attempt to evaluate any
pointed-to char object, just the pointers themselves.

Yes, unsigned char is generally better than char for this kind of
thing, but in this case there's no problem using char.  (Assuming that
"pover" and "p" are defined appropriately; "Jamie Boy" gave no hint of
what they are.)

--
Keith Thompson (The_Other_Keith) k@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.

No, you are right. It only traps when it is a "lvalue", which doesn't mean
"left hand side of the equation" but when you try to read it. So in this
case neither of the pointers are dereferenced and it can't trap. Just
pointing to it is OK. Additonally the rules have been changed on character
traps. It seems that they don't generate UB any more until you pass them to
a text stream or sprintf().
On Feb 14, 9:02 am, "asit dhal" <penasit@gmail.com> wrote:

> Can anyone tell how to measure the size of an array without use of
> sizeof operator ?

If you don't want to use the sizeof operator, two things that come to
mind are:

1) Keep track of the length of the array (or a pointer to the end) as
you build it, and that way you always know how big it is.

2) Put a sentinel value at the end of the array and the count the
number of elements from the start of the array until you hit the
sentinel.

The string library functions often work this second way, with a '\0'
character at the end of the array.  That's how they know when to stop
counting, copying, printing, or whatever.

-Beej

On 2?15?, ??5?21?, Richard Heathfield <r@see.sig.invalid> wrote:

very good!
On 2?15?, ??9?41?, "Beej" <b@beej.us> wrote:

very very good!!!!!!!!!!!!!!!
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc