Get array size by a pointer dynamically

Discussion in 'C Programming' started by terry, Nov 14, 2004.

  1. terry

    terry Guest

    Hi,

    Could anyone tell me how to determine the size of array of characters
    dynamically? For example,

    :
    :
    char *a[]={"hello","hi","kitty"};
    char *b[]={"orange","apple"};

    void main()
    {
    char **p=a;
    :
    :

    }

    I want to get array size of "a" which is 3 by "p". How?

    Thanks!
    terry, Nov 14, 2004
    #1
    1. Advertising

  2. terry <> scribbled the following:
    > Hi,


    > Could anyone tell me how to determine the size of array of characters
    > dynamically?


    Impossible in ISO standard C.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
    and vow excitedly zooming."
    - JIPsoft
    Joona I Palaste, Nov 14, 2004
    #2
    1. Advertising

  3. terry

    Trent Buck Guest

    Quoth terry on or about 2004-11-14:
    > Could anyone tell me how to determine the size of array of characters
    > dynamically? For example,
    >
    > char *a[]={"hello","hi","kitty"};
    >
    > I want to get array size of "a" which is 3 by "p". How?


    The usual method is to make the last element in the array a `special'
    value, such as 0 or -1 or EOF. For example (caveat: untested)

    void
    main (void)
    {
    char *x[] = { "foo", "bar", "baz", 0 };
    int idx, len;

    len = 0;
    for (idx = 0; x[idx]; ++idx)
    len++;

    printf ("Length is `%d'\n", len);
    }

    -trent
    Trent Buck, Nov 14, 2004
    #3
  4. terry

    Kurt Watzka Guest

    Trent Buck wrote:

    > Quoth terry on or about 2004-11-14:
    >> Could anyone tell me how to determine the size of array of characters
    >> dynamically? For example,
    >>
    >> char *a[]={"hello","hi","kitty"};
    >>
    >> I want to get array size of "a" which is 3 by "p". How?

    >
    > The usual method is to make the last element in the array a `special'
    > value, such as 0 or -1 or EOF. For example (caveat: untested)


    Why do you think this is the "usual" method? The usual method is to pass
    the size of the array along with a ponter to the first element. To avoid
    counting the elements in the array, you can use sizeof, both on the array
    and on its first element (sizeof a/sizeof a[0]), if the initialization of
    the array is visible.

    However, if the number of elements in "a" is determined by an
    initializer, there is nothing dynamic about it.

    >
    > void
    > main (void)


    This _may_ cause a compiler targeting a hosted environment to optimize away
    the whole program, or to put "bam" in x[3].

    > {
    > char *x[] = { "foo", "bar", "baz", 0 };
    > int idx, len;
    >
    > len = 0;
    > for (idx = 0; x[idx]; ++idx)
    > len++;
    >
    > printf ("Length is `%d'\n", len);
    > }
    >


    Kurt Watzka
    Kurt Watzka, Nov 14, 2004
    #4
  5. On Sun, 14 Nov 2004 00:20:19 -0800, terry wrote:

    > Hi,
    >
    > Could anyone tell me how to determine the size of array of characters
    > dynamically? For example,
    >
    > :
    > :
    > char *a[]={"hello","hi","kitty"};
    > char *b[]={"orange","apple"};
    >
    > void main()


    In C main returns int.

    > {
    > char **p=a;
    > :
    > :
    >
    > }
    >
    > I want to get array size of "a" which is 3 by "p". How?


    In C a pointer doesn't know anything about the size of an array it is
    pointing at. This is fundamental and means that even various standard
    library functions that need to know the size of an array require you to
    pass that size (e.g. fgets()). So the direct answer to your question to
    your question is that you can't.

    As others have said there are various techniques to work around this, e.g.
    putting a marker in the array data or maintaining a size variable
    alongside the pointer. And this is an issue specifically with pointers, if
    you have the ARRAY definition available you can get the size off that e.g.
    (sizeof a/sizeof *a) will evaluate to 3 for the definition of a above.

    Lawrence
    Lawrence Kirby, Nov 15, 2004
    #5
    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. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    338
    Tim Rentsch
    Oct 9, 2004
  2. Replies:
    10
    Views:
    580
    Keith Thompson
    Jun 10, 2007
  3. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    663
    Martin Ambuhl
    Jan 28, 2008
  4. Replies:
    29
    Views:
    1,030
    Keith Thompson
    Oct 10, 2008
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    441
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page