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

void display_record (char  name[10], int roll, int age )
{
  printf ("Student Name: %s\n", name);
  printf ("Roll No#%d, Age %d\n", s_ptr ->roll, s_ptr ->age);
}

Passing array in pointer style

void display_record (char * name, int roll, int age )
{
  printf ("Student Name: %s\n", name);
  printf ("Roll No#%d, Age %d\n", s_ptr ->roll, s_ptr ->age);
}

Here one thing to point is “array name is always treated as a pointer". Even if function argument char name[10] 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[10]; Then calculate the size with this calculation size = sizeof(name)/sizeof(name[0]);. But the rule may not apply here. Function argument array is always pointer thus even if we declare as “char name[10]’, still it is a pointer thus we are not allowed to do this conventional size calculation.

void display_record (char name[10], int roll, int age )
{
  int size = sizeof(name)/sizeof(name[0]);
}

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.

void display_record (char name[10], int name_len, int roll, int age )
{
  int size = name_len;
}

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.

void display_record (const char * name, int roll, int age )
{
  printf ("Student Name: %s\n", name);
  printf ("Roll No#%d, Age %d\n", s_ptr ->roll, s_ptr ->age);
}

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

library function linking, call-by-value and call-by-reference, parameter passing call-by-value, parameter passing call-by-reference, structure as function argument, array as function argument, inline-functions, macro vs inline-function, function pointer, default C++ argument value,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan