Integer array size

Discussion in 'C Programming' started by Jeff, Nov 12, 2003.

  1. Jeff

    Jeff Guest

    Hello everybody,

    I'm kind of new to C programming, but here's a little question.
    Usually, when you have an array of chars, you put a \0 at the end of
    it to terminate the string. That way, it is possible with functions
    like strlen to get the array size. But is it possible to do the same
    thing with an array of integers or floats? And are there any functions
    that do so ?

    Any help will be appreciated,
    Cheers!

    - Joseph
    Jeff, Nov 12, 2003
    #1
    1. Advertising

  2. In article <>, Jeff wrote:
    > Hello everybody,
    >
    > I'm kind of new to C programming, but here's a little question.
    > Usually, when you have an array of chars, you put a \0 at the end of
    > it to terminate the string. That way, it is possible with functions
    > like strlen to get the array size. But is it possible to do the same
    > thing with an array of integers or floats? And are there any functions
    > that do so ?


    Strictly speaking, strlen() doesn't return the length of the
    array, only the length of the string stored in the array.


    #include <string.h>
    #include <stdio.h>

    int main(void)
    {
    char text[] = "hello world";

    printf("Length is %d\n", strlen(text));
    text[2] = '\0';
    printf("Length is %d\n", strlen(text));

    return 0;
    }


    Note that even in the first printf() statement, strlen() lies
    about the array length as it doesn't count the last element.


    When it comes to arrays of other kinds it is very common to
    keep track of the length in another variable. It is extremly
    uncommon to not know exactly how long an array is at any
    particular point in time. Functions taking arrays as arguments
    almost always also takes an integer value describing its length.


    --
    Andreas Kähäri
    Andreas Kahari, Nov 12, 2003
    #2
    1. Advertising

  3. "Jeff" <> wrote in message
    news:...
    > Hello everybody,
    >
    > I'm kind of new to C programming, but here's a little question.
    > Usually, when you have an array of chars, you put a \0 at the end of
    > it to terminate the string. That way, it is possible with functions
    > like strlen to get the array size. But is it possible to do the same
    > thing with an array of integers or floats? And are there any functions
    > that do so ?
    >
    > Any help will be appreciated,
    > Cheers!
    >
    > - Joseph


    character array with trailing '\0' represent C strings. This is not
    applicable to integer/float arrays.

    Thanks
    Praveen
    sahukar praveen, Nov 12, 2003
    #3
  4. Greetings.

    In article <>, sahukar praveen wrote:
    > "Jeff" <> wrote in message
    > news:...
    >> I'm kind of new to C programming, but here's a little question.
    >> Usually, when you have an array of chars, you put a \0 at the end of
    >> it to terminate the string. That way, it is possible with functions
    >> like strlen to get the array size. But is it possible to do the same
    >> thing with an array of integers or floats? And are there any
    >> functions that do so ?

    >
    > character array with trailing '\0' represent C strings. This is not
    > applicable to integer/float arrays.


    Sure it is, in the general case. Adding a particular sentinel value to
    the end of an array is useful when passing arrays to functions that
    don't also take the array size as an argument.

    To the OP: There are no standard sentinel values for non-char arrays,
    and (partly therefore) no standard functions to return the number of
    assigned elements in non-char arrays. However, it's a rather simple
    matter to write your own functions to do so; simply have a pointer loop
    over the array until it reaches the sentinel value. Either keep a loop
    counter, or return the difference between the pointer value at the end
    of the loop and the pointer value at the beginning of the loop.

    Regards,
    Tristan

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
    Tristan Miller, Nov 12, 2003
    #4
  5. Tristan Miller wrote:
    > Greetings.
    >
    > In article <>, sahukar praveen wrote:


    >>character array with trailing '\0' represent C strings. This is not
    >>applicable to integer/float arrays.

    >
    >
    > Sure it is, in the general case. Adding a particular sentinel value to
    > the end of an array is useful when passing arrays to functions that
    > don't also take the array size as an argument.


    In the general case you cannot do this. In the specific cases of
    knowing that certain values will never be present in the array
    or being able to use home grown escape codes then this is possible
    to do. But not in the general case.

    With strings you know that 0 will never appear in the array so there
    is no problem.

    --
    Thomas.
    Thomas Stegen, Nov 12, 2003
    #5
  6. Greetings.

    In article <3fb21810$>, Thomas Stegen wrote:
    > Tristan Miller wrote:
    >> Greetings.
    >>
    >> In article <>, sahukar praveen wrote:

    >
    >>>character array with trailing '\0' represent C strings. This is not
    >>>applicable to integer/float arrays.

    >>
    >>
    >> Sure it is, in the general case. Adding a particular sentinel value
    >> to the end of an array is useful when passing arrays to functions
    >> that don't also take the array size as an argument.

    >
    > In the general case you cannot do this. In the specific cases of
    > knowing that certain values will never be present in the array
    > or being able to use home grown escape codes then this is possible
    > to do. But not in the general case.


    Yes. By "in the general case" in my post, I meant that the
    null-terminator technique can be generalized to an arbitrary
    terminator. That is, it's not possible to add a trailing '\0' to an
    array of float, though it is possible to use some specific
    floating-point value.

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
    Tristan Miller, Nov 12, 2003
    #6
  7. Jeff

    John Bode Guest

    (Jeff) wrote in message news:<>...
    > Hello everybody,
    >
    > I'm kind of new to C programming, but here's a little question.
    > Usually, when you have an array of chars, you put a \0 at the end of
    > it to terminate the string. That way, it is possible with functions
    > like strlen to get the array size. But is it possible to do the same
    > thing with an array of integers or floats? And are there any functions
    > that do so ?
    >
    > Any help will be appreciated,
    > Cheers!
    >
    > - Joseph


    Actually, strlen() returns the length of the string contained in the
    array, not the array size itself. For example, if you have

    char foo[10] = "bar";

    strlen(foo) returns 3, not 10.

    If you want to compute the number of elements in the array, you can do
    the following:

    size_t foo_len = sizeof foo / sizeof foo[0]

    This divides the total number of bytes contained in the array by the
    bytes contained in a single element, yielding the number of elements.
    Note that this method will only work for objects of array type; it
    will not work for dynamically allocated arrays, or for arrays passed
    as arguments to a function, because in those cases you are dealing
    with a pointer type, not an array type.

    #include <stdlib.h>

    int main (void)
    {
    int a1[20];
    int *a2;
    int e1, e2;

    a2 = malloc (sizeof *a2 * 20);
    e1 = sizeof a1 / sizeof a1[0]; /* 20 * sizeof int / sizeof int
    == 20 */
    e2 = sizeof a2 / sizeof a2[0]; /* sizeof int* / sizeof int !=
    20 */
    }
    John Bode, Nov 12, 2003
    #7
  8. On Wed, 12 Nov 2003, Jeff wrote:

    > Hello everybody,
    >
    > I'm kind of new to C programming, but here's a little question.
    > Usually, when you have an array of chars, you put a \0 at the end of
    > it to terminate the string. That way, it is possible with functions
    > like strlen to get the array size. But is it possible to do the same
    > thing with an array of integers or floats? And are there any functions
    > that do so ?


    The strlen function will not tell you the size of an array. It will tell
    you the size of the string within the array. For example:

    char s[10000] = "Bob";
    int length = strlen(s);

    The size of the array is 10000 but strlen will return the size of the
    string "Bob". There is an operator called sizeof that will work for you.
    For example:

    int array_length = sizeof s;

    This will set array_length to 10000.

    Since you are new to C language I'd suggest you read and understand the
    FAQ (Frequently Asked Questions) for this group. You can find it by
    searching for "comp.lang.c FAQ".

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
    Darrell Grainger, Nov 12, 2003
    #8
  9. Jeff

    Jeff Guest

    Thank-you very much for all the help. I had indeed thought of putting
    a certain value that would not appear normally, and then scan the
    array for that value to determine it's length, but I wanted to check
    if there was any 'standard'.

    Thanks again,

    - Joseph
    Jeff, Nov 12, 2003
    #9
    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. Replies:
    7
    Views:
    642
  2. Replies:
    5
    Views:
    530
    Flash Gordon
    Apr 9, 2006
  3. desktop

    Size of integer array

    desktop, Apr 24, 2007, in forum: C++
    Replies:
    5
    Views:
    378
    Salt_Peter
    Apr 24, 2007
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,559
    Michael Jung
    May 25, 2008
  5. Suresh V
    Replies:
    5
    Views:
    3,711
    SaticCaster
    Jul 5, 2010
Loading...

Share This Page