Re: How to know the size of array




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*/
}

.



Relevant Pages

  • Re: Newbie question about class member functions...
    ... mainalways returns int, never void. ... // Open playlist file and read it into an array ...
    (comp.lang.cpp)
  • Jtable repaint - it just doesnt work! tried
    ... public int numRows; ... public void setValueAt{ ... array, then plugs it into a jTable ... public void windowClosing(java.awt.event.WindowEvent evt) { ...
    (comp.lang.java.programmer)
  • Re: Mergesort Vs Quicksort
    ... given as variable-length records packed end-to-end in one array, ... int scmp(const void *a, const void *b); ...
    (comp.programming)
  • Re: quick sort
    ... void quicksort; ... int partition; ... and a populateIntArray(int *array, size_t size) function such ... printIntArray, quicksort() (yes, I'd have called it something ...
    (comp.lang.c)
  • Re: segfault w/ block, but not file scope
    ... void foo{ ... possess by-reference arguments, even ordinary local variables ... The "value" of the array is a pointer to the array's ...
    (comp.lang.c)