sizeof integer array

R

rahul8143

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));
}
 
S

Suman

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));
}
 
S

Suman

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));
}
 
A

Antonio Contreras

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.
 
K

Krishanu Debnath

Suman said:
(e-mail address removed) wrote:
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
 
S

Suman

Krishanu said:
Suman said:
(e-mail address removed) wrote:
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.
 
M

Martin Ambuhl

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top