473,770 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5668
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
1669
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 commandline. It worked great when i put the code in main(). Then why not, copy this piece in a function. But when i type in the text, pressed enter i got an segmentationfault("Speicherzugriffsfehler"). Excidently i only typed to charakter in an it...
42
2416
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
15
4520
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 namespace, but it fails when passed a managed type. I poked around the Reflection namespace, but found nothing that fits the bill. Is there a runtime equivalent to sizeof in ..NET?
24
1667
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
2061
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
7203
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 /**************************************/ #include<stdio.h>
20
2186
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 suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
15
1534
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
3118
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 function, size of return "1". ex int tab; printf("%d",sizeof(tab)/sizeof(int)); // print 10 int length(int *tab)
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.