0
#include<stdio.h>
#include<conio.h>

float smallest(int arr[],int k,int n);
void sort(int arr[],int n);

void main()
{
 int arr[20],i,n,j,k;
 clrscr();
 printf("\nEnter the number of elements in the array: ");
 scanf("%d",&n);

 printf("\nEnter the elements of the array");
 for(i=0 ; i < n ; i++)
 {
  printf("\n arr[%d] = ",i);
  scanf("%d",&arr[i]);
 }

 sort(arr,n);
 printf("\nThe sorted array is: \n");
 for(i=0 ; i < n ;  i++)
 printf("%d\t",arr[i]);
 getch();
}

int smallest(int arr[],int k,int n)//smallest function
{
 int pos=k,small=arr[k],i;
 for(i=k+1;i<n;i++)
 {
  if(arr[i]<small)
  {
   small=arr[i];
   pos=i;
  }
 }
 return pos;
}


void sort(int arr[],int n)//sorting function
{
 int k,pos,temp;
 for(k=0 ; k < n ; k++)
  {
   pos=smallest(arr,k,n);
   temp=arr[k];
   arr[k]=arr[pos];
   arr[pos]=temp;
  }
}

In the above program the sort function is being called from main but the return type of sort is void and it still returns the sorted array. As after sorting the array the function should return the sorted array back to the calling function to print the sorted array but the program runs perfectly. How is that happening?

10
  • you pass array to the function and the function works on that array directly since arrays in C are passed by reference. it doesn't return anything, it rather works on the argument being passed to it Commented Apr 11, 2014 at 5:47
  • @mangusta Arrays are not passed by reference. Commented Apr 11, 2014 at 5:50
  • 1
    @mangusta Wrong, everything is passed by value. Arrays decay to pointers. You cannot pass a function, only a function pointer. Commented Apr 11, 2014 at 5:53
  • 1
    @mangusta: you cannot say pass by reference, there is nothing in C as reference, you can rather say passed as pointer. As self tries to explain, in C only pass by value is there. Commented Apr 11, 2014 at 5:57
  • 1
    @all can any one post the correct answer? please. i think if arrays are passed by reference then there is no return required as said by mangusta Commented Apr 11, 2014 at 6:00

3 Answers 3

5

When you declare

int arr[20];

you can say "arr is an array of 20 integers". But arr is a pointer to an integer as well, pointing to the first integer in a row of 20. So de-referencing *arr is an integer, the same as arr[0] in fact.

This means when you pass arr to a function you only pass a pointer to that function. The function in this case works on the (copied) pointer. But this very pointer points exactly to the same memory as your original arr declared in main(). And that's the reason why manipulating arr in sort() is in fact manipulating arr in main().

Sign up to request clarification or add additional context in comments.

Comments

4

When passing an array as a parameter, this

int smallest(int arr[],int k,int n)

means exactly the same as

int smallest(int *arr,int k,int n)

For example

#include<iostream>
void printArray(int data[])
{
  for(int i = 0, length = sizeof(data); i < length; ++i)
  {
    std::cout << data[i] << ' ';
  }
  std::cout << std::endl;
}

int main()
{
  int data[] = { 5, 7, 8, 9, 1, 2 };
  printArray(data);
  return 0;
}

You will see that only the first 4 elements of the array are printed. The sizeof(data) returns a value of 4! That happens to be the size of the pointer used to pass the array to printArray(). First the array does not get copied. The pointer to the first element of the array is copied

1 Comment

@amit I hope now it's helpful.
0

First, there is no connection between any function argument what is, or is not passed using a return statement with an expression according to the function's return type.

While it is true that all parameter passing in C is by value - copy the value to a "local parameter variable" - nothing is assumed about what is to happen at the memory location a pointer is referencing. So, a function can make any changes in the calling environment, even without returning a value.

As to parameters declared as being aType name[]. this is merely syntactic sugar for const aType* name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.