473,729 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get the size of the array which is an argument of a function?


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 >

int upzeroH(double *inputH)
{
int i_data;
for(i_data=0;i_ data<1024;++i_d ata)
printf("%g\n",* (inputH+i_data) );

i_data=0;
while((inputH+i _data)!=(double *)0)
++i_data;

printf("Size of input data is %i\n",i_data);

return 0;

}

int main(void)
{
int i,j,row=1024,co lumn=3;
double a[row];
double dt=0.02;

for(i=0;i<row;+ +i)
{ a[i]=sin(i*dt);
printf("%g\n",a[i]);
}

printf("Size of array a is %i\n",sizeof(a)/sizeof(double)) ;
upzeroH(a);

return 0;
}

/*************** *************** *************** *
*/

In fact, in the function,the array has been printed successfully by printf statement.

I try to use the while statement to calculate the array size.But it is failed.

I just want to know whether the pointer (inputH+i_data) has reached the end of the array
by while((inputH+i _data)!=(double *)0), but it cannot work.

Is there any sign to indicate the end of an array? which I can use to quit the while loop.

Or, is it better to use another argument nCount=sizeof(a )/sizeof(double) to give the array size
directly?

Thanks for your help.


Apr 9 '07 #1
7 8130
On Apr 8, 7:55 pm, bowlderster <bowlders...@gm ail.comwrote:
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 >

int upzeroH(double *inputH)
{
int i_data;
for(i_data=0;i_ data<1024;++i_d ata)
printf("%g\n",* (inputH+i_data) );

i_data=0;
while((inputH+i _data)!=(double *)0)
++i_data;

printf("Size of input data is %i\n",i_data);

return 0;

}

int main(void)
{
int i,j,row=1024,co lumn=3;
double a[row];
double dt=0.02;

for(i=0;i<row;+ +i)
{ a[i]=sin(i*dt);
printf("%g\n",a[i]);
}

printf("Size of array a is %i\n",sizeof(a)/sizeof(double)) ;
upzeroH(a);

return 0;

}

/*************** *************** *************** *
*/

In fact, in the function,the array has been printed successfully by printf statement.

I try to use the while statement to calculate the array size.But it is failed.

I just want to know whether the pointer (inputH+i_data) has reached the end of the array
by while((inputH+i _data)!=(double *)0), but it cannot work.

Is there any sign to indicate the end of an array? which I can use to quit the while loop.

Or, is it better to use another argument nCount=sizeof(a )/sizeof(double) to give the array size
directly?

Thanks for your help.
You have to pass the size explicitly.

Apr 9 '07 #2
bowlderster wrote:
Hello,all.

I want to get the array size in a function, and the array is an argument of the function.
Pass the size to the function. Always check the FAQ before posting. In
this case you might want to start with question 6.21
<http://c-faq.com/aryptr/aryparmsize.htm l>
Apr 9 '07 #3
bowlderster <bo*********@gm ail.comwrote:
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 >
int upzeroH(double *inputH)
{
Sorry, but the array is _not_ the argument of the function. The
argument of the function is a pointer to the first element of
the array. Actually, you can't pass arrays to functions at all
in C, you can only pass a pointer to the first (or some other
element). In C an array is not an object that can be passed to
functions. The only composite objects you can pass to functions
are structures and unions, nothing else. And for that reason it
is impossible to figure out from within a function how many ar-
guments the array you used in calling the function had since the
function will only receive a pointer to an element of the array.
If you need the size of the array within the function you must
pass it as an additional argument.
int i_data;
for(i_data=0;i_ data<1024;++i_d ata)
printf("%g\n",* (inputH+i_data) );
i_data=0;
while((inputH+i _data)!=(double *)0)
++i_data;
This can't work. It assumes that somehow the address of the
element directly after the end of the array will be NULL,
but that isn't the case. Instead, you will have a completely
innocent looking address which doesn't indicate in any way
that it's an address past the end of the array.
printf("Size of input data is %i\n",i_data);
return 0;
}
int main(void)
{
int i,j,row=1024,co lumn=3;
double a[row];
double dt=0.02;

for(i=0;i<row;+ +i)
{ a[i]=sin(i*dt);
printf("%g\n",a[i]);
}
printf("Size of array a is %i\n",sizeof(a)/sizeof(double)) ;
It might be prudent to write the

sizeof(a)/sizeof(double)

as

sizeof a / sizeof *a

because you then don't have to change anything in case you
have to change the type of 'a'. (And you don't need paren-
theses around the "arguments" of sizeof when you have a
variable and not a type.)
upzeroH(a);
And here you do not pass the array as such but a pointer to the
first element. It may not look like this but that is how it works
in C. In C data can only be passed by value. And an array (con-
trary to "simple" types (like ints, doubles etc.)and structures
and unions) can't be passed by value. So when the argument is not
a "simple" type or a structure/union it must be somehow converted
to a value. And the way C deals with that is that in such circum-
stances (i.e. when a value is required, called "in value context")
the array is converted to it's first element. If you want to read
an in-depth explanation see Chris Toreks web page

http://web.torek.net/torek/c/pa.html
return 0;
}
/*************** *************** *************** *
*/
In fact, in the function,the array has been printed successfully by
printf statement.
Yes, but only because you used your knowledge that the original array
had 1024 elements.
I try to use the while statement to calculate the array size.But it
is failed.
I just want to know whether the pointer (inputH+i_data) has reached
the end of the array by while((inputH+i _data)!=(double *)0), but it
cannot work.
Correct. It can't work.
Is there any sign to indicate the end of an array? which I can use to
quit the while loop.
No. You have to pass the size of the array to the function simply
because the function does not receive "the array" but just a pointer
to its first element. (The only trick you could use would require
that there's a data value that can't be a valid value of the array,
then make array one element longer and put that into the last element.
That's how you determine the length of a string: since the '\0' cha-
racter is not a valid value in a string it thus can be used as an
indicator for the end of the string (wich otherwise would be just an
array of chars). But there lots of cases where that trick can't be
used since all values that can be stored in the elements are valid
values. Of course, if you have an array that only can have values
that are the sin() of an angle you could use a value larger than 1
or smaller than -1 as the "impossible " value that indicates the end
of the array - but this a special cases, it already wouldn't do any-
more if you would have instead an array with tan() values.)
Or, is it better to use another argument nCount=sizeof(a )/sizeof(double)
to give the array size directly?
This can only be used in the function where the array was defined
(or if the array is a global array). And there is no other method
to figure out the size of an array within a function that got pas-
sed an array simply because the function actually didn't got passed
the array but a pointer to its first element.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Apr 9 '07 #4
jt@toerring.de (Jens Thoms Toerring) writes:

Thank you very much.
The reply is very helpful for me.
I know a lot about array and pointer.

Best regards.
bowlderster <bo*********@gm ail.comwrote:
>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<stdli b.h>
#include<math. h>
>int upzeroH(double *inputH)
{

Sorry, but the array is _not_ the argument of the function. The
argument of the function is a pointer to the first element of
the array. Actually, you can't pass arrays to functions at all
in C, you can only pass a pointer to the first (or some other
element). In C an array is not an object that can be passed to
functions. The only composite objects you can pass to functions
are structures and unions, nothing else. And for that reason it
is impossible to figure out from within a function how many ar-
guments the array you used in calling the function had since the
function will only receive a pointer to an element of the array.
If you need the size of the array within the function you must
pass it as an additional argument.
> int i_data;
for(i_data=0;i_ data<1024;++i_d ata)
printf("%g\n",* (inputH+i_data) );
> i_data=0;
while((inputH+i _data)!=(double *)0)
++i_data;

This can't work. It assumes that somehow the address of the
element directly after the end of the array will be NULL,
but that isn't the case. Instead, you will have a completely
innocent looking address which doesn't indicate in any way
that it's an address past the end of the array.
> printf("Size of input data is %i\n",i_data);
return 0;
}
>int main(void)
{
int i,j,row=1024,co lumn=3;
double a[row];
double dt=0.02;

for(i=0;i<row;+ +i)
{ a[i]=sin(i*dt);
printf("%g\n",a[i]);
}
> printf("Size of array a is %i\n",sizeof(a)/sizeof(double)) ;

It might be prudent to write the

sizeof(a)/sizeof(double)

as

sizeof a / sizeof *a

because you then don't have to change anything in case you
have to change the type of 'a'. (And you don't need paren-
theses around the "arguments" of sizeof when you have a
variable and not a type.)
> upzeroH(a);

And here you do not pass the array as such but a pointer to the
first element. It may not look like this but that is how it works
in C. In C data can only be passed by value. And an array (con-
trary to "simple" types (like ints, doubles etc.)and structures
and unions) can't be passed by value. So when the argument is not
a "simple" type or a structure/union it must be somehow converted
to a value. And the way C deals with that is that in such circum-
stances (i.e. when a value is required, called "in value context")
the array is converted to it's first element. If you want to read
an in-depth explanation see Chris Toreks web page

http://web.torek.net/torek/c/pa.html
> return 0;
}
>/*************** *************** *************** *
*/
>In fact, in the function,the array has been printed successfully by
printf statement.

Yes, but only because you used your knowledge that the original array
had 1024 elements.
>I try to use the while statement to calculate the array size.But it
is failed.
>I just want to know whether the pointer (inputH+i_data) has reached
the end of the array by while((inputH+i _data)!=(double *)0), but it
cannot work.

Correct. It can't work.
>Is there any sign to indicate the end of an array? which I can use to
quit the while loop.

No. You have to pass the size of the array to the function simply
because the function does not receive "the array" but just a pointer
to its first element. (The only trick you could use would require
that there's a data value that can't be a valid value of the array,
then make array one element longer and put that into the last element.
That's how you determine the length of a string: since the '\0' cha-
racter is not a valid value in a string it thus can be used as an
indicator for the end of the string (wich otherwise would be just an
array of chars). But there lots of cases where that trick can't be
used since all values that can be stored in the elements are valid
values. Of course, if you have an array that only can have values
that are the sin() of an angle you could use a value larger than 1
or smaller than -1 as the "impossible " value that indicates the end
of the array - but this a special cases, it already wouldn't do any-
more if you would have instead an array with tan() values.)
>Or, is it better to use another argument nCount=sizeof(a )/sizeof(double)
to give the array size directly?

This can only be used in the function where the array was defined
(or if the array is a global array). And there is no other method
to figure out the size of an array within a function that got pas-
sed an array simply because the function actually didn't got passed
the array but a pointer to its first element.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Apr 9 '07 #5
On 9 Apr, 00:55, bowlderster <bowlders...@gm ail.comwrote:
I want to get the array size in a function,
and the array is an argument of the function.
<snip>
Is there any sign to indicate the end of an array?
which I can use to quit the while loop.

Or, is it better to use another argument
nCount=sizeof(a )/sizeof(double) to give the array size
directly?
2 common ways to do this are to pass the
size as another argument, and to use
a sentinel.

For example, both techniques are employed
in main(int argc, char **argv). Argc passes
the length of the array pointed to by argv,
and that array has a NULL terminator. (Note
that argv is not an array: it just points to
the first element of an array.)

Apr 9 '07 #6
bowlderster wrote:
Thank you very much.
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Apr 9 '07 #7
bowlderster wrote:
>
Thank you very much.
The reply is very helpful for me.
I know a lot about array and pointer.
Please do not top-post.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
--
Posted via a free Usenet account from http://www.teranews.com

Apr 9 '07 #8

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

Similar topics

14
3911
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
3
2877
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I call the function using this code fragement in my main function: --- char** arg_array; arg_count = create_arg_array(command, argument, arg_array); for(count = 0; count < arg_count; count++)
9
10689
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
18
2480
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { { 4.0, 2.0 }, { 5.0, 8.0 }, { 6.0, 6.0 },
12
112097
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>
29
2363
by: Vasileios Zografos | last post by:
Hi everyone, I need to build a function to plug it in a program (that I didnt make or can change) that should be called something like this: float someFunction(float x) { ...
15
484
by: RedLars | last post by:
Given this generic object; typedef struct { const char * name; int address; } Parameter; I defined the following array of Parameters; static Parameter para = {{"BUS", 1},{"Network", 102}, {NULL, 0}};
30
2683
by: Angel Tsankov | last post by:
Hello! Does the C++ standard define what happens when the size argument of void* operator new(size_t size) cannot represent the total number of bytes to be allocated? For example: struct S { char a; };
28
4573
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" <<...
0
9426
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
9281
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
9200
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
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 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...
0
6022
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2680
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.