-1

So here's my code.

#include<stdio.h>
 int main()
 {
  int a[4]={6,2,7,3};
  sort(a,4);
  int i;
  for(i=0;i<4;i++)
  printf("%d",a[i]);
 }
  int sort(int a[],int n)
  {
   int i,j,temp;
   for (i=0; i< (n -1); i++)
   {
    for(j = (i+1); j < n; j++)
    {
     if (a[i]< a[j])
     {
       temp= a[i];
       a[i] = a[j];
       a[j] = temp;
     }
    }
   }
  }

and the output is 7632. I thought a variable's value is never changed in main when passed to a function (unless using pointers). It's just photocopied and operations are done. But here the values of a[i]'s are changing in the main function. How does that work? I am not even returning the sorted array.

w.brian
  • 16,296
  • 14
  • 69
  • 118
user2316393
  • 89
  • 2
  • 2
  • 6
  • Welcome to stackoverflow. Please tag your qustions properly so that you can get helpful answers? how can one identify that this is the code for C, C++ or stream? – Dhaval Marthak Apr 24 '13 at 16:08
  • sorry.This is my first question.I'll do that. – user2316393 Apr 24 '13 at 16:13
  • If you want to pass arrays around by value in C++, consider `std::vector` or `std::array` instead. Unlike raw arrays, they copy themselves when passed by value. Unfortunately, i don't think C has (or even can have) a real equivalent, as there's no such thing as a copy constructor. You'd have to copy the array manually if you wanted to pass a copy. – cHao Apr 24 '13 at 16:17

2 Answers2

4

You are passing a pointer; arrays in function declarations decay to a pointer to their first element.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • and a definition of _array decay_ : http://stackoverflow.com/questions/1461432/what-is-array-decaying – linkdd Apr 24 '13 at 16:40
  • So you mean,if I try to pass an array,it automatically becomes a pointer? – user2316393 Apr 24 '13 at 17:00
  • And does it happen for any other things, or just arrays? – user2316393 Apr 24 '13 at 17:07
  • @user2316393 yes. If you want to actually pass an array by value, you need to wrap it in a `struct`. It's one of the more confusing historical accidents in C. See http://c-faq.com/aryptr/aryptrparam.html – ecatmur Apr 24 '13 at 17:07
  • @user2316393 Functions decay to function pointers, but not in function declarations. – ecatmur Apr 24 '13 at 17:08
0
int sort(int a[],int n)
{
    ...
}

a is a pointer even though you used []. You cannot pass arrays in C or C++, instead a pointer to the first element is passed.

john
  • 7,897
  • 29
  • 27
  • So you mean,if I try to pass an array,it automatically becomes a pointer? – user2316393 Apr 24 '13 at 17:02
  • @user2316393: Yep. Arrays "decay" to pointers automatically. At the drop of a hat, really. :P C-style arrays are rather bare-bones; they're basically just a block of contiguous, properly aligned, element-sized storage bins, and don't even carry info about how many bins. That makes copying much harder to do reliably. There are two fixes for that: make arrays include their length (turning them more into something like structs), or forget copying entirely and pass them around as pointers. C chose the latter. – cHao Apr 24 '13 at 17:54