How to know the size of array

M

manochavishal

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


Thanx in advance

Vishal
 
V

Vladimir S. Oka

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`.
 
M

manochavishal

void foo(int * array)


void foo(int *array, size_t len)



Now the array size is in `len`.

Thanx for the quick reply.

I think i read somewhere that i can get the size by:

(sizeof(array) ) / (sizeof(element))

in this case it will be

(sizeof(array)) / (sizeof(int))

I am not sure about this. Can we get the sizeof array to return the
size of all its elements.
Can sizeof find where the array is getting finished or it will just
give me the size of element.

Is there no way to get the size of array without passing the its
length.

Cheers
Vishal
 
S

Suman

I think i read somewhere that i can get the size by:

(sizeof(array) ) / (sizeof(element))

in this case it will be

(sizeof(array)) / (sizeof(int))

FYI:
[src = n1124.pdf]
---------------
6.5.3.4 The sizeof operator
....
Semantics
[ ...]
When applied to an operand that has array
type, the result is the total number of bytes in the array.[85]
[ ... ]
6. EXAMPLE 2 Another use of the sizeof operator is
to compute the number of elements in an array:

sizeof array / sizeof array[0]
....
[85] When applied to a parameter declared to have array or function
type, the sizeof operator yields the size of the adjusted (pointer)
type.
----------
 
S

santosh

Thanx for the quick reply.
I think i read somewhere that i can get the size by:

(sizeof(array) ) / (sizeof(element))

No. This yields the number of elements in the array, not it's size.
in this case it will be

(sizeof(array)) / (sizeof(int))

I am not sure about this. Can we get the sizeof array to return the
size of all its elements.
Can sizeof find where the array is getting finished or it will just
give me the size of element.

The sizeof operator can give the sizes of all compile time data
structures. For those that you declare at runtime, you'll have to
manage their size yourself.
Is there no way to get the size of array without passing the its
length.

As Vladimir has noted, you can use a "sentinel" value and scan through
the array for this special value to find it's length and hence it's
size. C's strings are implemented in this way.
 
V

Vladimir S. Oka

Thanx for the quick reply.

I think i read somewhere that i can get the size by:

You could have read it in my reply as well, have you scrolled all the
way down. Do that now.
(sizeof(array) ) / (sizeof(element))

This is not the preferred syntax. See your textbook.
in this case it will be

(sizeof(array)) / (sizeof(int))

Look at my previous post, interspersed with your code.
I am not sure about this. Can we get the sizeof array to return the
size of all its elements.
Can sizeof find where the array is getting finished or it will just
give me the size of element.

Look at other replies in this thread.
Is there no way to get the size of array without passing the its
length.

Use the "sentinel" if you have a value that cannot happen otherwise. I
told you that already.
 
P

pemo

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!
 
K

Keith Thompson

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 can't. sizeof() inside the function applies to the pointer, not
to the array.

Do you want the size (in bytes) or the length (in elements)?

Section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>, is likely
to be helpful.
 
K

Keith Thompson

pemo said:
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};

...
[...]

Again, it's important to distinguish between the *size* (in bytes) and
the *length* (in elements).

That approach is usable only if the element type is capable of holding
the length. For character arrays, this limits the maximum length to
255 on most systems. For non-numeric arrays, it's just not workable.

Passing a separate argument is a much more general solution.
 
Z

zhousqy

u should pass the size of array exlicitly or place a 'NULL' value at
the end of the array.
 
S

santosh

u should pass the size of array exlicitly or place a 'NULL' value at
the end of the array.

Please quote the post to which you're replying and avoid abbreviations
like 'u' for you etc. Read the following:

http://cfaj.freeshell.org/google/

NULL is a macro which expands to the null pointer constant. A sentinel
value to signal the end of an array would usually contain a null
character which is different.
 
J

John Bode

Thanx for the quick reply.

I think i read somewhere that i can get the size by:

(sizeof(array) ) / (sizeof(element))

in this case it will be

(sizeof(array)) / (sizeof(int))

Given the following:

int foo[10];

sizeof foo will return the total number of bytes taken up by the whole
array, and sizeof foo[0] will return the number of bytes used by a
single array element, so yes, the expression

sizeof foo / sizeof foo[0]

will give you the number of elements (10) in the array.

Unfortunately, this won't help you if you pass the array to a function.
In most contexts*, the type of the array identifier "decays" into a
pointer to the base type, and its value is set to the address of the
first element in the array. IOW, when you write

bar(foo);

what actually gets passed to bar is a pointer type object, not an array
type object:

void bar(int *arr)
{
...
}

and sizeof arr will return the number of bytes used by the pointer,
*not* the array it points to, so the sizeof arr / sizeof arr[0] trick
won't work.

* The only times the array identifier doesn't decay into a pointer is
when it's an operand of the sizeof and unary & operators.
I am not sure about this. Can we get the sizeof array to return the
size of all its elements.
Can sizeof find where the array is getting finished or it will just
give me the size of element.

Is there no way to get the size of array without passing the its
length.

Not really, no.
 
K

Keith Thompson

santosh said:
Please quote the post to which you're replying and avoid abbreviations
like 'u' for you etc. Read the following:

http://cfaj.freeshell.org/google/

NULL is a macro which expands to the null pointer constant. A sentinel
value to signal the end of an array would usually contain a null
character which is different.

Not if it's an array of pointers. (Lacking context, I don't know what
the original array looked like.)
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top