473,473 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I get the size of an input array

62 New Member
Hi,

I tried the following code:

Expand|Select|Wrap|Line Numbers
  1. void SizeArray(int t[]){
  2.     cout << "Size of the input array: " << sizeof(t) << endl;
  3. }
  4. void main(void){
  5.     int arr[20];
  6.     SizeArray(arr);
  7. }
  8.  
It gives 4 as result instead 80. As I see in this case the "t" variable behaves like a pointer. My question is: how can I get the real size(80) of the input array?

Thanks

Arepi
Sep 11 '10 #1
3 3661
weaknessforcats
9,208 Recognized Expert Moderator Expert
The sizeof operator gives yiou the soze of the variable
Expand|Select|Wrap|Line Numbers
  1. on the stack
.

Therefore:

Expand|Select|Wrap|Line Numbers
  1. int arr[20];
  2.       sizeof(arr); 
  3.  
will have a sizeof 80 since the array is local (on the stack). The 80 is 20 x 4 assuming int is 4 bytes.

However:

Expand|Select|Wrap|Line Numbers
  1. void function(int arr[20])
  2. {
  3.    sizeof(arr);
will show arr with a sizeof 4.

This is because the name of an array is the address of element 0. In the function, the argument is the address of an array of 20 int. It is the sizeof the address you see. Addresses are 4 bytes in 32-bit operating systems.

I suggest you go to the C/C++ Insights forum and read the article Arrays Revealed where all this is explained.
Sep 11 '10 #2
Arepi
62 New Member
Thanks,
I know that in main it gives 80 and I mentioned in the function it behaves like a pointer.
Well I have to ask other: Can I anyway get the size of the elements wich ones pointed by a pointer or is possible give an array as real array for the function as argument?
I want get the size of input array in the given function(For example: I want read all elemnts of the array one by one that is why I need how long is the aray). Is it possible other then I solve the size in main and use this size as a second argument of the function?

Thanks

Arepi
Sep 11 '10 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
The customary solution is to pass the function the address of element 0 of the array plus a second argument for the number of elements (not the size of the array).

Old C libraries used and array of char as a special case. They passed the address of element 0 cas a char* and the function processed elements until it reached a binary 0 (\0) and then it stopped. This special array is called a C string.

You could structure your own array so that the last element of the array was some special value and then you could test for the element rather than passing the number of elments as a second argument.
Sep 12 '10 #4

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

Similar topics

2
by: Jason Morehouse | last post by:
Hello... I need to get the values (for validation) of a php style html input array: <input type="file" name="image"> <input type="file" name="image"> <input type="file" name="image"> <input...
8
by: Tee | last post by:
Hi, How do we increase the size of array on runtime and preserve the previous data? I don't want to use ArrayList because the array could be a multi-dimension array. Thanks, Tee
7
by: ultr | last post by:
I need a large 3D array of structures: struct s { char a; int b; }; s s_array; s_array is declared as global.
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
1
by: =?Utf-8?B?RmFyc2Fk?= | last post by:
Hi how can I declared initial size of array in structure. I have below code in c++ and translate this to vb.net. but get this error: "Arrays declared as structure members cannot be declared with...
3
satyanagendra
by: satyanagendra | last post by:
Hi please tell me how to find max size of array in turbo c if any body know about this please give me reply
8
by: johnehein | last post by:
#include <vector> using namespace std; template <typename Iter> int foo(Iter first, Iter last, int nn) { const size_t n = last - first; double buf; return 0;
16
by: danieleghisi | last post by:
Hello I'm writing a small programme to look for some particular integer sets (homometric sets), and each one of this sets is a unsigned long long (e.g. {1, 2, 4, 7} --1001011=75). I need to have...
7
crystal2005
by: crystal2005 | last post by:
Hi everyone, I gonna ask how to set unlimited size of array of characters. According to the tutorial that i have found after amount of time of googling, array size should be defined when we...
6
by: jacekr | last post by:
hi, What I want to do, is to print/generate n-size int array like this: int array; 0 0 0 0 0 1 0 0 2
0
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,...
0
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
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
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...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
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.