Full array not passed in C

I am working on sorting an insert, and my array in main () seems to be partially passed to sort (). The snippet below shows what testin main () matters {2, 1, 3, 1, 2}, but arrin sort () matters {2, 1}. What's going on here?

 #include <stdio.h>

 int sort(int* arr) {
      int i = 0;
      int j, key;
      int count = 0;

      printf("Inside sort(): ");
      for (j = 0; j < sizeof(arr)/sizeof(int); ++j)
           printf("%d ", arr[j]);
      printf("\n");

      for (j = 1; i < sizeof(arr)/sizeof(int); ++j) {
           key = arr[j];
           i = j - 1;
           while (i >= 0 && arr[i] > key) {
                arr[i + 1] = arr[i];
                --i;
                ++count;
           }
           arr[i + 1] = key;
      }
      return count;
 }

 int main(int argc, char* argv) {
      int test[] = {2, 1, 3, 1, 2};
      int i = 0;
      printf("Inside main(): ");
      for (i = 0; i < sizeof(test)/sizeof(int); ++i)
           printf("%d ", test[i]);
      printf("\n");
      int count = sort(test);
 }
+5
source share
1 answer

The idiom sizeof(arr)/sizeof(int)works only for statically distributed arrays and only within the scope that defines them.

In other words, you can use it for arrays like:

int foo[32];

... , . , , . , .

+10

All Articles