i am learning arrays i want to whats the following effect of cods while passing array to the following function
void fun(array[])
void fun(array[4])
void fun(array[],4)
void fun(array[5],4)
please help me .thank you in advance
i am learning arrays i want to whats the following effect of cods while passing array to the following function
void fun(array[])
void fun(array[4])
void fun(array[],4)
void fun(array[5],4)
please help me .thank you in advance
The first and third can never work. The second sends a specific array element to a function, while the fourth sends a specific array element and a separate integer.
thanks understood.what is mean passing entire array element and passing element by element.answer with example are appreciated Thank you in advance
"passing entire array element" doesn't mean anything at all. Neither does "passing element by element". You can pass an array (the whole thing) by giving its name (without any punctuation), or you can pass a particular element (by specifying which element to pass using [brackets]). And of course, that has to match what the function expects.
Code:int foo[10]; void fun1(int bar[]); //expects an array void fun2(int bar); //expect an int fun1(foo); //works fun2(foo[7]); //works fun1(foo[]); //error: foo[] is meaningless fun2(foo[]); //error: foo[] is meaningless fun1(foo[7]); //error: fun1 expects array, not int fun2(foo); //error: fun2 expects int, not array
actuall fun1 expects array.you too passing array of specific element then it works rightfun1(foo[7]); //error: fun1 expects array, not int
here fun 2 expects int but you passing array.how this worksfun2(foo[7]); //works
No, lets look at another example
Hopefully that explains why both of your remarks are incorrect.Code:int foo[10]; foo[7] = 22; fun1(foo[7]); //this passes the value 22 into fun1 because thats what foo[7] equals. However fun1 wants an array so this is a failed call fun2(foo[7]); //Again we pass in the value 22 which is exactly what fun2 wants since it only wants an int value so this call succeeds.
Last edited by Ewiv; 01-08-2014 at 10:34 AM.
in void func i have declared size of array x has 0.but it displaying value of x[2],x[3].how is thisCode:#include <iostream>#include <time.h> #include <stdlib.h> using namespace std; void func(int x[0])//my doubt is here { cout<<x[2]<<x[3]; } int main() { int a[4]={1,2,3,5}; func(a); return 0; }
You're accessing memory still, that will display random numbers. Very undesirable behavior.
In a function declaration (and only there), the size of an array* is irrelevant. You can always pass an array of any size to a function that declares it takes an array. In this context (only) x[0] means "any array", not "an array of size zero".
*When you have a multi-dimensional array, only the first size is irrelevant; the rest must be specified (and specified correctly for what is passed in to it).
thanks Tabstop .You gave a clear view. what do you mean is "void func(int x[0]) or "void func(int x[3])" what ever the size of array it just consider to take an array.that is what u meaning am i right
This article covers various techniques when passing arrays to functions along with pitfalls: SourceForge.net: Safer arrays in Cpp - cpwiki
I actually still disagree with that article quite heavily.
Take for example this code.
The comment about the assert is wrong. Mainly, I think the author put the cart before the horse.Code:#include <iostream> #include <assert.h> template <typename type, size_t N> size_t SizeOf ( type (&)[N] ) // parameter not used, omitted name to inhibit this warning { return N; } void foo(int arr[10], int& a, size_t size) { assert(size == 10); // <--- Bad things happen if a programmer forgets this assert. std::cout << "a is: " << a << std::endl; arr[9] = 100; std::cout << "a is: " << a << std::endl; } int main() { int a = 10; int arr[7]; foo(arr, a, SizeOf(arr)); }
If the intent of the function foo is to use an array of ten ints and only an array of ten ints then something like:
actually enforces what needs to be enforced. C arrays in C and C++, by design, decay into pointers so int[10] means int* and 10 is ignored.Code:void foo( std::array<int, 10> );
Bad things might happen if you forget the assert, but a better solution, I think, is to make foo work with any size array, or just abandon C interfacing. According to the article, it's "ugly" and in my opinion the solutions are kludges.
Something like this is also neater.Code:void foo(int *first, int *const last);
Last edited by whiteflags; 01-09-2014 at 07:48 PM.
Well, the article's examples should probably be rewritten to loops doing stuff with the array. That makes more sense, but doesn't change the dangers of not knowing the array bounds.
However, your range-like function has its downsides: it pushes the checking of valid ranges to the caller, which causes more bugs. Additionally, depending on how the function is written, you may prevent compiler optimizations because compilers have a harder time tracking pointers than array indices.