In our previous example we have seen structure can be passed via call by value or call by reference and it is the responsibility of the developer to use it properly. Now comes the case of array. Are these two facilities available here also? The answer is no. We have only call by reference available for passing an array to a function.
Earlier we have seen the fact that struct can be passed via call by value. Compiler copies the entire content of structure to stack and passes to the subroutine. Now suppose we assume this call by value facility is also there for array. In this case compiler has to copy entire content to stack for calling the subroutine and this may be feasible for structure as size will not be that much but for array size can be in any range thus it is not possible.
Considering all these facts array name is always treated as a pointer. C Compiler has been designed for this. Thus passing array means always passing as pointer.
Passing array in array style
Passing array in pointer style
Here one thing to point is “array name is always treated as a pointer". Even if function argument char name has been declared as an char array but compiler will still treat it as char pointer “char * name". Now some confusion may arise here. We often declare global or local array as char name; Then calculate the size with this calculation size = sizeof(name)/sizeof(name);. But the rule may not apply here. Function argument array is always pointer thus even if we declare as “char name’, still it is a pointer thus we are not allowed to do this conventional size calculation.
After this calculation we get the size = (32bit or 4 byte)/1byte = 4 (not 10) This is true as per compiler but surely not as per our conception of array calculation. We need to be very careful for this. We cannot pass the size using this. We need to provide a second argument along with this to pass the size and call has to fill this properly.
Another thing to consider is passing array only for reading. Here also like structure we can declare the pointer as const if we are using this only for reading purpose.
About our authors: Team EQA
You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Calculate weighted average