Re: How to know the size of array
- From: "pemo" <usenetmeister@xxxxxxxxx>
- Date: Thu, 23 Mar 2006 11:23:55 -0000
manochavishal@xxxxxxxxx wrote:
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>
void foo(int * array);
int main(void)
{
int n=5;
int array[n];
foo(array);
}
void foo(int * array)
{
/*Here how can i know the size of array without passing it
explicitly*/ }
You have to pass the size of the array somehow.
#include <stdio.h>
void arr(int size, int array[size])
{
printf("%d\n", size);
}
int main(void)
{
int n = 200;
int array[n];
arr(sizeof(array) / sizeof(array[0]), array);
return 0;
}
*However*
I'd like to add a thought here - for completeness really:
I expect most replies will be along the lines of 'you must pass the size' -
usually meaning as a seperate argument to the called function [as above].
However, the necessary size 'information' may be made available in a number
of ways.
Premise: you must make the size of the array available to the called
function.
However, the size information *could* be /passed/ in /n/ number of alternate
ways - for example, there could be a 'stop element' containing a value that
signifies that the end of the array has been reached [-1 below]. But, that
would of course mean that your array cannot contain a non-stop data value
of -1 [this will act like a '\0' for a char array used as a string].
#include <stdio.h>
void arr(int array[static 1]) // or void arr(int array[])
{
for(int n = 0; array[n] != -1; ++n)
{
printf("%d ", array[n]);
}
}
int main(void)
{
int array[11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1};
Output: 1 2 3 4 5 6 7 8 9 10
Or, a little safer, the first element could be used to indicate the size of
the array in total [Pascal(ish)].
void arr(int array[static 1]) // or void arr(int array[])
{
for(int n = 1; n < array[0]; ++n)
{
printf("%d ", array[n]);
}
}
int main(void)
{
int array[11] = {11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
...
Output: 1 2 3 4 5 6 7 8 9 10
I'm not sure about the legality of this, but that could be refined a little
....
#include <stdio.h>
#define arr_size(x) (sizeof(x) / sizeof(x[0]))
void arr(int array[static 1]) // or void arr(int array[])
{
for(int n = 1; n < array[0]; ++n)
{
printf("%d ", array[n]);
}
}
int main(void)
{
int array[11] = {arr_size(array), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Of course, encoding information about the array *in* the array itself could
well be, um, *problematic* [/read/: come as an unwelcome surprise to most C
programmers], but I thought it worth chucking it it!
--
==============
Not a pedant
==============
.
- Follow-Ups:
- Re: How to know the size of array
- From: Keith Thompson
- Re: How to know the size of array
- References:
- How to know the size of array
- From: manochavishal@xxxxxxxxx
- How to know the size of array
- Prev by Date: Re: How to know the size of array
- Next by Date: Re: linking question
- Previous by thread: Re: How to know the size of array
- Next by thread: Re: How to know the size of array
- Index(es):
Relevant Pages
|