473,385 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Nov 15 '05 #1
7 5646
ra*******@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));
}


Nov 15 '05 #2

ra*******@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));
}


Nov 15 '05 #3
ra*******@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.

Nov 15 '05 #4
Suman wrote:
ra*******@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

Nov 15 '05 #5

Krishanu Debnath wrote:
Suman wrote:
ra*******@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.

Nov 15 '05 #6
ra*******@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.
Nov 15 '05 #7
ra*******@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"
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kimi | last post by:
Hello, yes i try to learn c-programming.So i have a question on my code, but if i am wrong here please tell me a group where i belong to. I wrote a function which fgets a filename from the...
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
15
by: stand__sure | last post by:
Having recently had a need to use ZeroMemory from the kernel32.dll, I "discovered" that there is apparently no .NET equivalent to the c/c++ sizeof operator -- one does exist in the Marshall...
24
by: vandana | last post by:
Output of sizeof(int) varies.But on what it depends.Can anybody please help me to find out if it is compiler dependent or operating system dependent?
34
by: Raman | last post by:
Hi All, Is ut possible to calculate size of any standard data types at run time i.e without using sizeof() operator Thanks and Regards, Raman
58
by: Nishu | last post by:
Hi All, When I run the below program in MSVC, I get the output as 1 4 Could you tell me why sizeof 'A' is taken as 4? Is it standard defined or compiler specific? Thanks, Nishu...
20
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
15
by: SM | last post by:
Hello, I have another simple question about an array in PHP and a variable in PHP. This is the array: $thumbs_cat_1 = array( 'wine', 'cheese', 'ice',
7
by: alternative451 | last post by:
Hi, I have just one question : how to know the size of an array, i have un little program, i use first static, ant i can use sizeof() to know the size, but when i put it as paremeter in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.