Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   sizeof integer array (http://www.velocityreviews.com/forums/t438988-sizeof-integer-array.html)

rahul8143@gmail.com 08-05-2005 07:54 AM

sizeof integer array
 
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));
}


Suman 08-05-2005 08:34 AM

Re: sizeof integer array
 
rahul8143@gmail.com 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 08-05-2005 08:48 AM

Re: sizeof integer array
 

rahul8143@gmail.com 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));
> }



Antonio Contreras 08-05-2005 08:52 AM

Re: sizeof integer array
 
rahul8143@gmail.com 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.


Krishanu Debnath 08-05-2005 09:04 AM

Re: sizeof integer array
 
Suman wrote:
> rahul8143@gmail.com 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


Suman 08-05-2005 09:08 AM

Re: sizeof integer array
 

Krishanu Debnath wrote:
> Suman wrote:
> > rahul8143@gmail.com 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.


Martin Ambuhl 08-05-2005 09:13 AM

Re: sizeof integer array
 
rahul8143@gmail.com 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.

Emmanuel Delahaye 08-06-2005 09:01 AM

Re: sizeof integer array
 
rahul8143@gmail.com wrote on 05/08/05 :
> why following 2 codes does not give same result that is 10?


Because an array is not a pointer (and reverse).

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"




All times are GMT. The time now is 07:01 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.