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

B

bowlderster

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=sin(i*dt);
printf("%g\n",a);
}

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.
 
S

Siddhartha Gandhi

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=sin(i*dt);
printf("%g\n",a);
}

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.
 
J

Jens Thoms Toerring

bowlderster said:
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=sin(i*dt);
printf("%g\n",a);
}

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
 
B

bowlderster

(e-mail address removed) (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 said:
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=sin(i*dt);
printf("%g\n",a);
}

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
 
B

Bill Pursell

I want to get the array size in a function,
and the array is an argument of the function.
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.)
 
C

CBFalconer

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top