473,659 Members | 3,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding size of an integer array after the array has been passed to a function

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. class Test 
  6. {
  7. public:
  8.     void search(int arr[]) {
  9.         int count=0;
  10.         int size=(sizeof arr/sizeof arr[0]);
  11.         cout << "In search function, size = " <<size<<endl;
  12.         cout << "In search function, sizeof arr = "<<sizeof arr<<endl;
  13.         cout << "In search function, sizeof arr[0] = "<<sizeof arr[0]<<endl;
  14.     }
  15. };
  16.  
  17. void main () 
  18. {
  19.     int arr1[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
  20.     int siz=(sizeof arr1/sizeof arr1[0]);
  21.     cout << "In main, size = " <<siz<<endl;
  22.     cout << "In main, sizeof arr1 = " <<sizeof arr1<<endl;
  23.     cout << "In main, sizeof arr1[0] = " <<sizeof arr1[0]<<endl;
  24.     Test obj;
  25.     obj.search(arr1);
  26.     cin.get();
  27. }
  28.  
I am trying to find the size of an array after it is passed into a function. I used the sizeof operator. It displays the correct information in the main function, but as soon as I pass the array to a different function, it doesn't print the correct values. How do I fix this? Thanks.
Sep 28 '10 #1
2 1883
Oralloy
988 Recognized Expert Contributor
Use template vectors.

Basically the main function knows the size of the array, because it is instantiated there.

In your method search(), all the compiler knows is that it has an indefinite size array.

In C++ (and C) the compiler silently coerces indefinite array definitions into pointers when they are used as part of a function or method's argument list.

Expand|Select|Wrap|Line Numbers
  1. void function(double f[])
silently becomes

Expand|Select|Wrap|Line Numbers
  1. void function(double *f)
and thus (on most compilers) the sizeof f will be four.

Cheers!
Sep 28 '10 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
sizeof returns the size of the item on the stack.

Therefore, sizeof a local array will produce the correct result.

However, when an array is passed to a function, what is passed is the address of element 0. Therefore, inside the function, sizeof is reporting the size of the address and not the size of the array.

This is called decay of array.

The usual practice is to pass additional argument(s) for the number of elements.
Sep 28 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
11947
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int SizeOfArray(int a) { return (sizeof(a)/sizeof(a)); } main() { int a={1,2,3}; printf("%d\n",SizeOfArray(a));
22
2449
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now I have two questions - one is historical, and the other practical. 1.) Surely malloc (and...
3
5681
by: shyam | last post by:
Hello to all C geeks My query is as follows i declare a variable in main as UINT8 *c if i check for sizeof(c) it returns 20, i.e 20/4 = 5 elements. This is fine .
1
1472
by: Carlos Villaseñor M. | last post by:
Hi everybody! I finally have success implementing my first DLL function, I have exported a C++ function with a DLL using the "Declare" statement in Basic, but now I have a doubt, how can I to pass an array in the DLL function parameters list, I need to pass y return the same array, but changed, to my Basic application. Below I show my code in Basic:
14
3380
by: asit dhal | last post by:
Can anyone tell how to measure the size of an array without use of sizeof operator ?
8
5642
by: The Cool Giraffe | last post by:
I have a dynamically declared array of integers and i'd like to check the number of elements. For some reason, the expected sizeof(mtx)/sizeof(mtx) doesn't work. I've been googling and i got a head ache more than understanding. Let me put my question this way. In Java, i'd go as mtx.length to learn the size. How can it be done _easily_ in C++? The shorter the better. :)
7
8127
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h> #include<stdlib.h> #include<math.h>
9
11912
by: Grey Alien | last post by:
If I have a struct declared as : struct A { double x ; char name; struct Other other ; void * ptr ; };
28
4561
Nepomuk
by: Nepomuk | last post by:
Hi! I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work withsizeof(array)/sizeof(array)Now, this does work in some situations, but not in others. Here's what I mean:#include <iostream> int length(int * array){return sizeof(array)/sizeof(array);} int main() { int array1 = {3,2,1,0}; std::cout << "Length of array1: " << sizeof(array1)/sizeof(array1) << "\n" <<...
24
2933
by: arnuld | last post by:
I want to create a new array of the same size of an array already available: #include <stdio.h> #include <string.h> int main(void) { char arrc = "URI";
0
8335
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
8851
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
8747
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...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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
5649
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();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.