473,480 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Create 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_data)
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,column=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 8117
On Apr 8, 7:55 pm, bowlderster <bowlders...@gmail.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_data)
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,column=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.html>
Apr 9 '07 #3
bowlderster <bo*********@gmail.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_data)
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,column=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*********@gmail.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_data)
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,column=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...@gmail.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.securityfocus.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
3884
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
2833
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...
9
10674
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
2419
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 = { {...
12
112065
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
2329
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},...
30
2647
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 {...
28
4538
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...
0
7041
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
6908
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
7044
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
7084
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
4779
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
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
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
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.