Array size using pointer

Discussion in 'C Programming' started by dati_remo@libero.it, Oct 11, 2004.

  1. Guest

    Hi, is it possible to find the dimension of an array using a pointer?

    main()
    {
    int a[10];

    f(a);

    return;
    }


    f(int *b)
    {
    /*how can I know here the size of b??*/
    /*because sizeof(b) is always 4*/
    /*and sizeof(b[0]) is always 4*/

    ......
    }

    Thank you

    Dati
    , Oct 11, 2004
    #1
    1. Advertising

  2. Artie Gold Guest

    wrote:
    > Hi, is it possible to find the dimension of an array using a pointer?


    No.

    >
    > main()

    int main() /* implicit `int' was eliminated in C99 */
    > {
    > int a[10];
    >
    > f(a);

    f(a, sizeof a / sizeof *a);
    >
    > return;
    > }
    >
    >
    > f(int *b)

    void f(int *b, size_t n)
    > {
    > /*how can I know here the size of b??*/


    By passing it as an argument.

    > /*because sizeof(b) is always 4*/
    > /*and sizeof(b[0]) is always 4*/
    >
    > .....
    > }
    >

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas

    "If you don't think it matters, you're not paying attention."
    Artie Gold, Oct 11, 2004
    #2
    1. Advertising

  3. wrote:
    > Hi, is it possible to find the dimension of an array using a pointer?
    >
    > main()
    > {
    > int a[10];
    >
    > f(a);
    >
    > return;
    > }
    >
    >
    > f(int *b)
    > {
    > /*how can I know here the size of b??*/
    > /*because sizeof(b) is always 4*/
    > /*and sizeof(b[0]) is always 4*/


    Both ought to be the same, since both are pointers.
    And you are trying to get the size of a pointer variable
    (which is implementation - dependent).

    As Artie had already suggested, pass the length
    explicitly as an argument to the function.


    --
    Karthik. http://akktech.blogspot.com .
    ' Remove _nospamplz from my email to mail me. '
    Karthik Kumar, Oct 11, 2004
    #3
  4. Guest

    Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
    > wrote:
    > > Hi, is it possible to find the dimension of an array using a pointer?
    > >
    > > main()
    > > {
    > > int a[10];
    > >
    > > f(a);
    > >
    > > return;
    > > }
    > >
    > >
    > > f(int *b)
    > > {
    > > /*how can I know here the size of b??*/
    > > /*because sizeof(b) is always 4*/
    > > /*and sizeof(b[0]) is always 4*/

    >
    > Both ought to be the same, since both are pointers.
    > And you are trying to get the size of a pointer variable
    > (which is implementation - dependent).
    >
    > As Artie had already suggested, pass the length
    > explicitly as an argument to the function.


    I know, but I can't do it.

    Do you thing it is possible to find in the stack this information?

    I mean, I have the position of the first element of the array, then,
    moving up and down in the stack, I find this number. Or is the stack a
    simple storage for the program and only it knows this information.

    And then how does really operate sizeof? Has it an internal table with
    all variables?

    Thank you

    Best Regards

    Dati Remo
    , Oct 12, 2004
    #4
  5. Flash Gordon Guest

    On 12 Oct 2004 00:37:26 -0700
    () wrote:

    > Karthik Kumar <> wrote in message
    > news:<416adc48$1@darkstar>...
    > > wrote:
    > > > Hi, is it possible to find the dimension of an array using a
    > > > pointer?
    > > >
    > > > main()
    > > > {
    > > > int a[10];
    > > >
    > > > f(a);
    > > >
    > > > return;
    > > > }
    > > >
    > > >
    > > > f(int *b)
    > > > {
    > > > /*how can I know here the size of b??*/
    > > > /*because sizeof(b) is always 4*/
    > > > /*and sizeof(b[0]) is always 4*/

    > >
    > > Both ought to be the same, since both are pointers.
    > > And you are trying to get the size of a pointer variable
    > > (which is implementation - dependent).
    > >
    > > As Artie had already suggested, pass the length
    > > explicitly as an argument to the function.

    >
    > I know, but I can't do it.


    In that case you are in trouble.

    > Do you thing it is possible to find in the stack this information?


    No.

    1) The C programming language does not require the use of a stack.
    2) There is no reason for the compiler to store the size of the array on
    the stack so it is unlikely that it will.
    3) As soon as you move a pointer out of an object you have created and
    deference it (the only way I can think of for you to examine the stack
    if there is one) you have entered the realms of undefined behaviour and
    anything could happen.

    > I mean, I have the position of the first element of the array, then,
    > moving up and down in the stack, I find this number. Or is the stack a
    > simple storage for the program and only it knows this information.


    No. Forget it. Either find a way to pass the size in, find a way of
    including an "end of array" marker like the 0 used to mark the end of
    strings, or find another language.

    > And then how does really operate sizeof? Has it an internal table with
    > all variables?


    sizeof could be implemented in any way the compiler writer chooses.
    However, IMHO, since the compiler has to know the size of the object in
    any case (how else can it allocate enough space for it?) the compiler
    can just look up that size internally and substitute in the appropriate
    value at compile time.
    --
    Flash Gordon
    Sometimes I think shooting would be far too good for some people.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Oct 12, 2004
    #5
  6. wrote:
    > Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
    >
    >> wrote:
    >>
    >>>Hi, is it possible to find the dimension of an array using a pointer?
    >>>
    >>>main()
    >>>{
    >>>int a[10];
    >>>
    >>>f(a);
    >>>
    >>>return;
    >>>}
    >>>
    >>>
    >>>f(int *b)
    >>>{
    >>>/*how can I know here the size of b??*/
    >>>/*because sizeof(b) is always 4*/
    >>>/*and sizeof(b[0]) is always 4*/

    >>
    >> Both ought to be the same, since both are pointers.
    >>And you are trying to get the size of a pointer variable
    >>(which is implementation - dependent).
    >>
    >> As Artie had already suggested, pass the length
    >>explicitly as an argument to the function.

    >
    >
    > I know, but I can't do it.


    Why ? What is the problem ? After all, you are creating the array
    and you should know its dimensions.

    >
    > Do you thing it is possible to find in the stack this information?
    >
    > I mean, I have the position of the first element of the array, then,
    > moving up and down in the stack, I find this number.


    How ? You would land up in *undefined behaviour* territory the moment
    you access beyond the array boundary.

    > Or is the stack a
    > simple storage for the program and only it knows this information.


    1. The C standard does not talk of stack / heap (though most of the
    popular implementations do ).

    2. The memory manager does not even come into the picture when you
    are talking about sizeof. It is the compiler's job to determine
    its value.

    >
    > And then how does really operate sizeof? Has it an internal table with
    > all variables?



    <OT>
    In compiler design, it is called *symbol table*. And a given
    compiler evaluates sizeof at compile-time thanks to the symbol table,
    it (the compiler system) would have generated. But then, we are
    discussing about the implementation of C. it outside the
    scope of the C standard.
    </OT>


    --
    Karthik. http://akktech.blogspot.com .
    'Remove _nospamplz from my email to mail me.'
    Karthik Kumar, Oct 12, 2004
    #6
  7. wrote:
    > Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
    >
    >> As Artie had already suggested, pass the length
    >>explicitly as an argument to the function.

    >
    >
    > I know, but I can't do it.


    Assuming the reason is that you have control of the implementation
    but not the interface one thing you can do is to store the size
    somewhere the function can access it. A file scope variable of suitable
    linkage should do the trick. This should be a last resort solution
    as variables with file scope are bad except when they are good...
    It also smells like a hack, a workaround for architectural limits of
    the design. If you do not have control of the design you should find
    out whether these limits are there for a good reason or just because
    your particular need was not thought when created. If you do _not_ have
    control of the code that calls the function that needs the size then
    you are fubared with respect to standard C. Try asking in a group which
    is dedicated to your compiler perhaps.

    Otherwise, give some more context and we'll help you if we can.

    --
    Thomas.
    Thomas Stegen, Oct 12, 2004
    #7
  8. John Bode Guest

    () wrote in message news:<>...
    > Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
    > > wrote:
    > > > Hi, is it possible to find the dimension of an array using a pointer?
    > > >
    > > > main()
    > > > {
    > > > int a[10];
    > > >
    > > > f(a);
    > > >
    > > > return;
    > > > }
    > > >
    > > >
    > > > f(int *b)
    > > > {
    > > > /*how can I know here the size of b??*/
    > > > /*because sizeof(b) is always 4*/
    > > > /*and sizeof(b[0]) is always 4*/

    > >
    > > Both ought to be the same, since both are pointers.
    > > And you are trying to get the size of a pointer variable
    > > (which is implementation - dependent).
    > >
    > > As Artie had already suggested, pass the length
    > > explicitly as an argument to the function.

    >
    > I know, but I can't do it.
    >


    Why not?

    > Do you thing it is possible to find in the stack this information?
    >


    No. Seriously, there is no (portable) way to find out the physical
    size of an array based on a pointer to the first element alone. You
    have two choices:

    1. Save the size of the array and either pass it as an argument
    or save it in a file scope variable.

    2. Write a special sentinel value to the last element of the
    array,
    like how C strings are arrays of char terminated with a 0.
    This
    method is far more error prone, though, and won't necessarily
    tell
    you the *physical* size of the array, just how many elements
    come
    before the sentinel.

    Actually, there's a third choice: pass a pointer to the array (which
    is not the same thing as passing a pointer to the first element):

    int f(int (*b)[10])
    {
    /* sizeof *b == sizeof (int) * 10 */
    }

    int main (void)
    {
    int a[10];

    f(&a);
    return 0;
    }

    The only problem is that this assumes you're only working with arrays
    with 10 elements, so it's not very flexible.

    > I mean, I have the position of the first element of the array, then,
    > moving up and down in the stack, I find this number. Or is the stack a
    > simple storage for the program and only it knows this information.
    >


    Aside from the fact that not all machines even *use* a stack for
    passing arguments to functions, what you're passing is a *pointer*
    type, not an array type. As far as the called function is concerned,
    b is a pointer to a single int object, not an array. That object may
    be the first element of an array. It may be the last element of an
    array. It may be a scalar variable. There is simply no way for the
    called function to know based on the pointer alone.

    > And then how does really operate sizeof? Has it an internal table with
    > all variables?
    >


    Possibly. Or it could do something completely different. It's up to
    the compiler writer to decide how sizeof actually gets implemented.

    > Thank you
    >
    > Best Regards
    >
    > Dati Remo
    John Bode, Oct 12, 2004
    #8
  9. Guest

    (John Bode) wrote in message news:<>...
    > () wrote in message news:<>...
    > > Karthik Kumar <> wrote in message news:<416adc48$1@darkstar>...
    > > > wrote:
    > > > > Hi, is it possible to find the dimension of an array using a pointer?
    > > > >
    > > > > main()
    > > > > {
    > > > > int a[10];
    > > > >
    > > > > f(a);
    > > > >
    > > > > return;
    > > > > }
    > > > >
    > > > >
    > > > > f(int *b)
    > > > > {
    > > > > /*how can I know here the size of b??*/
    > > > > /*because sizeof(b) is always 4*/
    > > > > /*and sizeof(b[0]) is always 4*/
    > > >
    > > > Both ought to be the same, since both are pointers.
    > > > And you are trying to get the size of a pointer variable
    > > > (which is implementation - dependent).
    > > >
    > > > As Artie had already suggested, pass the length
    > > > explicitly as an argument to the function.

    > >
    > > I know, but I can't do it.
    > >

    >
    > Why not?
    >
    > > Do you thing it is possible to find in the stack this information?
    > >

    >
    > No. Seriously, there is no (portable) way to find out the physical
    > size of an array based on a pointer to the first element alone. You
    > have two choices:
    >
    > 1. Save the size of the array and either pass it as an argument
    > or save it in a file scope variable.
    >
    > 2. Write a special sentinel value to the last element of the
    > array,
    > like how C strings are arrays of char terminated with a 0.
    > This
    > method is far more error prone, though, and won't necessarily
    > tell
    > you the *physical* size of the array, just how many elements
    > come
    > before the sentinel.
    >
    > Actually, there's a third choice: pass a pointer to the array (which
    > is not the same thing as passing a pointer to the first element):
    >
    > int f(int (*b)[10])
    > {
    > /* sizeof *b == sizeof (int) * 10 */
    > }
    >
    > int main (void)
    > {
    > int a[10];
    >
    > f(&a);
    > return 0;
    > }
    >
    > The only problem is that this assumes you're only working with arrays
    > with 10 elements, so it's not very flexible.
    >
    > > I mean, I have the position of the first element of the array, then,
    > > moving up and down in the stack, I find this number. Or is the stack a
    > > simple storage for the program and only it knows this information.
    > >

    >
    > Aside from the fact that not all machines even *use* a stack for
    > passing arguments to functions, what you're passing is a *pointer*
    > type, not an array type. As far as the called function is concerned,
    > b is a pointer to a single int object, not an array. That object may
    > be the first element of an array. It may be the last element of an
    > array. It may be a scalar variable. There is simply no way for the
    > called function to know based on the pointer alone.
    >
    > > And then how does really operate sizeof? Has it an internal table with
    > > all variables?
    > >

    >
    > Possibly. Or it could do something completely different. It's up to
    > the compiler writer to decide how sizeof actually gets implemented.
    >
    > > Thank you
    > >
    > > Best Regards
    > >
    > > Dati Remo



    Thanks to all.

    I'll try other solutions.

    Dati Remo
    , Oct 13, 2004
    #9
  10. Groovy hepcat Karthik Kumar was jivin' on Mon, 11 Oct 2004 12:13:11
    -0700 in comp.lang.c.
    Re: Array size using pointer's a cool scene! Dig it!

    > wrote:
    >> Hi, is it possible to find the dimension of an array using a pointer?
    >>
    >> main()
    >> {
    >> int a[10];
    >>
    >> f(a);
    >>
    >> return;


    This return statement is pointless.

    >> }
    >>
    >> f(int *b)
    >> {
    >> /*how can I know here the size of b??*/
    >> /*because sizeof(b) is always 4*/
    >> /*and sizeof(b[0]) is always 4*/

    >
    > Both ought to be the same, since both are pointers.


    That is incorrect. b is a pointer, but b[0] is an int.

    >And you are trying to get the size of a pointer variable
    >(which is implementation - dependent).


    No, he is trying to determine the size of an array from a pointer
    pointing at the array's first element. But, as has been stated time
    and time again here, it is impossible.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Oct 15, 2004
    #10
    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:
    348
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    674
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,251
  4. Hansen
    Replies:
    3
    Views:
    1,101
    rep_movsd
    Apr 24, 2010
  5. , India

    pointer to an array vs pointer to pointer

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

Share This Page