Re: How to know the size of array
- From: "Vladimir S. Oka" <novine@xxxxxxxxxxxxxxx>
- Date: 23 Mar 2006 02:14:46 -0800
manochavishal@xxxxxxxxx wrote:
Hi,
I have a question.
How can i know the size of array when it is passed to a function.
You can't, unless:
a) you pass it as an extra parameter
b) last element of your array is a "sentinel" value so you can
determine it at run-time (like C strings which are zero terminated).
For Example i have this code:
#include <stdio.h>
#include <stdlib.h>
void foo(int * array);
Try:
void foo(int *array, size_t len);
int main(void)
{
int n=5;
int array[n];
Not allowed in C90. Try:
#define ARRAY_SIZE 5
int array[ARRAY_SIZE];
foo(array);
foo(array, sizeof array / sizeof array[0]);
}
void foo(int * array)
void foo(int *array, size_t len)
{
Now the array size is in `len`.
/*Here how can i know the size of array without passing it explicitly*/
}
.
- Follow-Ups:
- Re: How to know the size of array
- From: manochavishal@xxxxxxxxx
- 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: syntax: struct in a struct
- Next by Date: Re: How to know the size of array
- Previous by thread: How to know the size of array
- Next by thread: Re: How to know the size of array
- Index(es):
Relevant Pages
|