473,889 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to know the size of array


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>

void foo(int * array);

int main(void)
int n=5;
int array[n];



void foo(int * array)

/*Here how can i know the size of array without passing it explicitly*/
Thanx in advance


Mar 23 '06
12 112125
zhou...@gmail.c om wrote:
u should pass the size of array exlicitly or place a 'NULL' value at
the end of the array.

Please quote the post to which you're replying and avoid abbreviations
like 'u' for you etc. Read the following:


NULL is a macro which expands to the null pointer constant. A sentinel
value to signal the end of an array would usually contain a null
character which is different.

Mar 24 '06 #11

ma***********@g mail.com wrote:
void foo(int * array)

void foo(int *array, size_t len)


Now the array size is in `len`.

Thanx for the quick reply.

I think i read somewhere that i can get the size by:

(sizeof(array) ) / (sizeof(element ))

in this case it will be

(sizeof(array)) / (sizeof(int))

Given the following:

int foo[10];

sizeof foo will return the total number of bytes taken up by the whole
array, and sizeof foo[0] will return the number of bytes used by a
single array element, so yes, the expression

sizeof foo / sizeof foo[0]

will give you the number of elements (10) in the array.

Unfortunately, this won't help you if you pass the array to a function.
In most contexts*, the type of the array identifier "decays" into a
pointer to the base type, and its value is set to the address of the
first element in the array. IOW, when you write


what actually gets passed to bar is a pointer type object, not an array
type object:

void bar(int *arr)

and sizeof arr will return the number of bytes used by the pointer,
*not* the array it points to, so the sizeof arr / sizeof arr[0] trick
won't work.

* The only times the array identifier doesn't decay into a pointer is
when it's an operand of the sizeof and unary & operators.
I am not sure about this. Can we get the sizeof array to return the
size of all its elements.
Can sizeof find where the array is getting finished or it will just
give me the size of element.

Is there no way to get the size of array without passing the its
Not really, no.


Mar 24 '06 #12
"santosh" <sa*********@gm ail.com> writes:
zhou...@gmail.c om wrote:
u should pass the size of array exlicitly or place a 'NULL' value at
the end of the array.

Please quote the post to which you're replying and avoid abbreviations
like 'u' for you etc. Read the following:


NULL is a macro which expands to the null pointer constant. A sentinel
value to signal the end of an array would usually contain a null
character which is different.

Not if it's an array of pointers. (Lacking context, I don't know what
the original array looked like.)

Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 24 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same! 9: More about Arrays 10: More about Pointers What blows me out of the water is the fact that 'every' programmer
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...
by: madireddy | last post by:
Hi, Inside a program how do i find out what size has been allocated to pointer using malloc. eg... int *p; p=(int*)malloc(1024*sizeof(int)); now how do find how much has been allocated to p? mojozoox!
by: Phill | last post by:
Anyone know the internal working of this thing. I expected it to be kinda slow at enumerating large lists of objects but it was actually pretty good. I was able to write a quick linked list class that beat it at enumerating but was not as fast as the arrayList at actually adding the items into the list. This makes me think it is using something like linked arrays. Where instead of allocating a new node each time an object is added it...
by: lovecreatesbeauty | last post by:
/* How do free() know how many elements should be freed in a dynamic array? When free a single variable, the amount of byte of memory can be retrieved from the type of variable itself. Now, suppose there is an array of more than one element need to be freed, as free() doesn't accept a argument indicating the size, how can
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
by: Harry | last post by:
Good Day To all, When i am declaring a array for e.g char ....it means i am declaring array of 45 characters each of which has a maximum,minimum value limit or range...for example in VC++(compiler that i am using,it has nothing to do with C++) char's range is -128 to +127....this is the range for each char element..... My doubt is what is the upper limit for the array size(45 is the size here) and what decides it.....
by: desktop | last post by:
I have a function that takes two pointers to an array. The first point to the first element while the other points to the last element. int nums = { 1, 2, 3, 4, 5, 7, 8, 9}; int* result; int end = (sizeof(nums)/4)-1; int s = 7; result = ::myfind(nums, nums + end, s);
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...
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...
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,...
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...
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...
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();...
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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.

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.