473,503 Members | 12,103 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 1875
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
11937
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...
22
2425
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...
3
5673
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
1468
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...
14
3359
by: asit dhal | last post by:
Can anyone tell how to measure the size of an array without use of sizeof operator ?
8
5636
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...
7
8120
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>...
9
11905
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
4542
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...
24
2915
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
7098
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...
0
7296
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,...
0
7364
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...
1
7017
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...
0
7470
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...
0
4696
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...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
751
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.