how to measure size of an array

Discussion in 'C Programming' started by asit dhal, Feb 14, 2007.

  1. asit dhal

    asit dhal Guest

    Can anyone tell how to measure the size of an array without use of
    sizeof operator ?
     
    asit dhal, Feb 14, 2007
    #1
    1. Advertising

  2. asit dhal

    Ben Pfaff Guest

    "asit dhal" <> 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\
    );}return 0;}
     
    Ben Pfaff, Feb 14, 2007
    #2
    1. Advertising

  3. asit dhal

    Guest

    On Feb 14, 9:02 am, "asit dhal" <> 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.
     
    , Feb 14, 2007
    #3
  4. asit dhal

    Jamie Boy Guest

    asit dhal:

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



    (char*)pover - (char*)p



    --
    ~/JB\~
     
    Jamie Boy, Feb 14, 2007
    #4
  5. "asit dhal" <> 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) <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.
     
    Keith Thompson, Feb 14, 2007
    #5
  6. asit dhal

    Joe Wright Guest

    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 ---
     
    Joe Wright, Feb 14, 2007
    #6
  7. said:

    > On Feb 14, 9:02 am, "asit dhal" <> 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.
     
    Richard Heathfield, Feb 14, 2007
    #7
  8. "Jamie Boy" <> 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.
    > --
    > ~/JB\~
     
    Malcolm McLean, Feb 14, 2007
    #8
  9. asit dhal

    Ben Pfaff Guest

    "Malcolm McLean" <> writes:

    > "Jamie Boy" <> 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\
    );}return 0;}
     
    Ben Pfaff, Feb 14, 2007
    #9
  10. asit dhal

    Flash Gordon Guest

    Malcolm McLean wrote, On 14/02/07 22:50:
    > "Jamie Boy" <> 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
     
    Flash Gordon, Feb 14, 2007
    #10
  11. "Malcolm McLean" <> writes:
    > "Jamie Boy" <> 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) <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.
     
    Keith Thompson, Feb 15, 2007
    #11
  12. "Ben Pfaff" <> wrote in message
    > "Malcolm McLean" <> writes:
    >
    >> "Jamie Boy" <> 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?
    >

    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().
     
    Malcolm McLean, Feb 15, 2007
    #12
  13. asit dhal

    Beej Guest

    On Feb 14, 9:02 am, "asit dhal" <> 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
     
    Beej, Feb 15, 2007
    #13
  14. asit dhal

    Guest

    On 2ÔÂ15ÈÕ, ÉÏÎç5ʱ21·Ö, Richard Heathfield <> wrote:
    > said:
    >
    > > On Feb 14, 9:02 am, "asit dhal" <> 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/1999http://www.cpax.org.uk
    > email: rjh at the above domain, - www.


    very good!
     
    , Feb 15, 2007
    #14
  15. asit dhal

    Guest

    On 2ÔÂ15ÈÕ, ÉÏÎç9ʱ41·Ö, "Beej" <> wrote:
    > On Feb 14, 9:02 am, "asit dhal" <> 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


    very very good!!!!!!!!!!!!!!!
     
    , Feb 15, 2007
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Fallon
    Replies:
    5
    Views:
    3,552
    Joe Fallon
    Jul 28, 2005
  2. Oliver Wong
    Replies:
    3
    Views:
    457
    Robert Klemme
    May 30, 2006
  3. Replies:
    5
    Views:
    585
    Flash Gordon
    Apr 9, 2006
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,743
    Michael Jung
    May 25, 2008
  5. Max Williams
    Replies:
    4
    Views:
    124
    Max Williams
    Nov 3, 2009
Loading...

Share This Page