sizeof integer array

Discussion in 'C Programming' started by rahul8143@gmail.com, Aug 5, 2005.

  1. Guest

    hello,
    why following 2 codes does not give same result that is 10?
    1)
    #include <stdio.h>
    #define DIM(array) sizeof(array)/sizeof(int)
    void main()
    {
    int arr[10];
    printf("The dimension of the array is %d", DIM(arr));
    }


    2)
    #include <stdio.h>
    int DIM(int array[])
    {
    return (sizeof(array)/sizeof(int));
    }
    void main()
    {
    int arr[10];
    printf("The dimension of the array is %d", DIM(arr));
    }
    , Aug 5, 2005
    #1
    1. Advertising

  2. Suman Guest

    wrote:
    > hello,
    > why following 2 codes does not give same result that is 10?
    > 1)
    > #include <stdio.h>
    > #define DIM(array) sizeof(array)/sizeof(int)

    #define DIM(array) sizeof array / sizeof array[0]
    is probably better.
    > void main()

    use int main()
    > {
    > int arr[10];
    > printf("The dimension of the array is %d", DIM(arr));
    > }
    >
    >
    > 2)
    > #include <stdio.h>
    > int DIM(int array[])

    should ideally return size_t.
    > {
    > return (sizeof(array)/sizeof(int));
    > }

    array is no longer the object you have in main, just a copy.
    > void main()
    > {
    > int arr[10];
    > printf("The dimension of the array is %d", DIM(arr));
    > }
    Suman, Aug 5, 2005
    #2
    1. Advertising

  3. Suman Guest

    wrote:
    > hello,
    > why following 2 codes does not give same result that is 10?
    > 1)
    > #include <stdio.h>
    > #define DIM(array) sizeof(array)/sizeof(int)
    > void main()
    > {
    > int arr[10];
    > printf("The dimension of the array is %d", DIM(arr));
    > }
    >
    >
    > 2)
    > #include <stdio.h>
    > int DIM(int array[])
    > {
    > return (sizeof(array)/sizeof(int));


    >From the draft of the C99 standard:


    When applied to a parameter declared to have array [...] type,
    the sizeof operator yields the size of the adjusted (pointer) type.

    > }
    > void main()
    > {
    > int arr[10];
    > printf("The dimension of the array is %d", DIM(arr));
    > }
    Suman, Aug 5, 2005
    #3
  4. wrote:
    > hello,
    > why following 2 codes does not give same result that is 10?
    > 1)
    > #include <stdio.h>
    > #define DIM(array) sizeof(array)/sizeof(int)
    > void main()
    > {
    > int arr[10];
    > printf("The dimension of the array is %d", DIM(arr));
    > }
    >
    >
    > 2)
    > #include <stdio.h>
    > int DIM(int array[])
    > {
    > return (sizeof(array)/sizeof(int));
    > }
    > void main()
    > {
    > int arr[10];
    > printf("The dimension of the array is %d", DIM(arr));
    > }


    When you pass an array as a parameter to a function it decays into a
    pointer to the first element of the array. In the second case, inside
    the DIM function, sizeof(array) is equivalent to siezof(int *), which
    is the reason why the second method does not give the correct answer.

    BTW, it would've been nice of your part to post the results of each
    program.
    Antonio Contreras, Aug 5, 2005
    #4
  5. Suman wrote:
    > wrote:


    <snip>

    > > 2)
    > > #include <stdio.h>
    > > int DIM(int array[])

    > should ideally return size_t.
    > > {
    > > return (sizeof(array)/sizeof(int));
    > > }

    > array is no longer the object you have in main, just a copy.


    Nonsense. array decay to pointer to first element of actual 'array'
    in function parameter context.

    Krishnau
    Krishanu Debnath, Aug 5, 2005
    #5
  6. Suman Guest

    Krishanu Debnath wrote:
    > Suman wrote:
    > > wrote:

    >
    > <snip>
    >
    > > > 2)
    > > > #include <stdio.h>
    > > > int DIM(int array[])

    > > should ideally return size_t.
    > > > {
    > > > return (sizeof(array)/sizeof(int));
    > > > }

    > > array is no longer the object you have in main, just a copy.

    >
    > Nonsense. array decay to pointer to first element of actual 'array'
    > in function parameter context.
    >

    Poor wording on my part, which is why I posted a second time.
    Suman, Aug 5, 2005
    #6
  7. wrote:
    > hello,
    > why following 2 codes does not give same result that is 10?
    > 1)
    > #include <stdio.h>
    > #define DIM(array) sizeof(array)/sizeof(int)
    > void main()

    ^^^^
    You're dead already.
    Martin Ambuhl, Aug 5, 2005
    #7
  8. Emmanuel Delahaye, Aug 6, 2005
    #8
    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. Derek
    Replies:
    7
    Views:
    24,307
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    617
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,393
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    545
    Joe Smith
    May 22, 2006
  5. Replies:
    46
    Views:
    633
    Shao Miller
    Jan 14, 2013
Loading...

Share This Page