passing size of an array to a function
...,you must pass to the function the array size. (Note:Another practise is to pass a开发者_如何学C pointer to the beginning of the array and a pointer to the location just beyond the end of the array.The difference of the two pointers is the length of the array and resulting code is simpler.)
I couldn't write the codes for what it's saying in the note. Can anyone help me?
The problem is that if you cannot pass an array to a function. When you do so, the name of the array "decays" to a pointer to its first element. That means you no longer know the size of that array, as you can only pass around a pointer to an element in that array. So you have to pass it a size, so the function receiving the "array" knows how long it is.
e.g.
//the below declaration is even the same as e.g.
//void foo(unsigned char array[4], size_t length)
//the array notation doesn't really apply to a function argument list
//it'll mean a pointer anyhow.
void foo(unsigned char *array, size_t length)
{
size_t i;
for(i = 0; i < length ; i++) {
printf("%02X ", array[i]);
}
}
Or in the case where you pass in a pointer one element past the end of the array:
void bar(unsigned char *array, unsigned char *end)
{
while(array != end) {
printf("%02X ", *array);
array++;
}
}
And you call it like
unsigned char data[] = {1,2,3,4};
foo(data,sizeof data); //same as foo(&data[0], siseof data);
bar(data, data + 4); //same as bar(&data[0], &data[4]);
Something like this:
int accumulate( int *first, int *last )
{
int result = 0;
for( ; first != last; ++first ) {
result += *first;
}
return result;
}
int main()
{
int arr[] = {1,2,3,4,5};
int sum = accumulate( arr, arr + 5 );
return 0;
}
The code above sums all the elements in the range [first, last)
; where first
in included in the range and last
is excluded. So you need to pass a pointer to the beginning of the range you want to sum and another to one location past the last element you want added to the sum.
Let's assume we have this array of integers:
#define CAPACITY 42
int foo[CAPACITY];
And these two functions that process it:
void func1(int* begin, int* end) {
// Loop over the array
while(begin < end) {
int val = *begin++; // process element
}
}
void func2(int* begin, int size) {
// Loop over the array
for(int i = 0; i < size; ++i) {
int val = begin[i]; // process element
}
}
The first one would be called like func1(foo, foo + CAPACITY)
: passing in a pointer to the start of the array, and a pointer to just beyond the last element.
The second would be called like func2(foo, CAPACITY)
: passing in a pointer to the start of the array and the array size.
Due to how pointer arithmetic works, both versions are equivalent.
void f(int* arr, int len) { ... }
void f(int* start, int* end) { ... }
If you only want to pass the array size you could do pass the following to the function :
function(sizeof(array)/sizeof(array[0]));
int A[] = {1,2,3,4,5,6,7,8,9,0};
printf("Size of the Array %d \n", sizeof(A)/sizeof(int));//size of the array
you can use this value to function
void getSize(int A[], size_t size){
//do somthing
}
void function()
{
//from where you call this
int A[] = {1,2,3,4,5,6,7,8,9,0};
getSize(A, sizeof(A)/sizeof(int));
}
精彩评论