-
-
#include<iostream>
-
using namespace std;
-
-
class Test
-
{
-
public:
-
void search(int arr[]) {
-
int count=0;
-
int size=(sizeof arr/sizeof arr[0]);
-
cout << "In search function, size = " <<size<<endl;
-
cout << "In search function, sizeof arr = "<<sizeof arr<<endl;
-
cout << "In search function, sizeof arr[0] = "<<sizeof arr[0]<<endl;
-
}
-
};
-
-
void main ()
-
{
-
int arr1[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
-
int siz=(sizeof arr1/sizeof arr1[0]);
-
cout << "In main, size = " <<siz<<endl;
-
cout << "In main, sizeof arr1 = " <<sizeof arr1<<endl;
-
cout << "In main, sizeof arr1[0] = " <<sizeof arr1[0]<<endl;
-
Test obj;
-
obj.search(arr1);
-
cin.get();
-
}
-
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.
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. - void function(double f[])
silently becomes
and thus (on most compilers) the sizeof f will be four.
Cheers!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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 .
|
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...
|
by: asit dhal |
last post by:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?
| |
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...
|
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>...
|
by: Grey Alien |
last post by:
If I have a struct declared as :
struct A
{
double x ;
char name;
struct Other other ;
void * ptr ;
};
|
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...
|
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";
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |