I wanted to implement sorting by insertion and asked myself this question: how do I pass an array to a function so that after processing its values ​​do not change in the main function? Suppose I want to pass an array to a function so that it is sorted there, output it in the same function, but so that its values ​​do not change in the main function. If when I pass a regular variable to a function, it will not change, and if the array passes a pointer to the array, it will change its values ​​to the main function.

#include <stdio.h> #include <stdlib.h> #include <ctime> #include <iostream> void PrintA(int A[], int N); void Swap(int *a, int *b); void InsertionSort(int A[], int N); int main(void) { const int N = 10; int A[N]; for (int i = 0; i < N; i++) { A[i] = 1 + rand() % 9; } printf("Array under insertion sort: \n"); PrintA(A, N); InsertionSort(A, N); /*printf("Array after insertion sort: \n"); PrintA(A, N);*/ system("pause"); } void PrintA(int A[], int N) { for (int i = 0; i < N; i++) { printf("%d ", A[i]); } printf("\n"); } void Swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void InsertionSort(int A[], int N) { int j; for (int i = 1; i < N; i++) { j = i; while (j > 0 && A[j - 1] > A[j]) { Swap(&A[j - 1], &A[j]); j--; } } PrintA(A, N); } 

I tried to somehow remove the pointer to the array, but then I had errors with the Swap function in the InsertionSort function.

  • 2
    No What you want in C / C ++ languages ​​is not provided. Make a copy with your own pens. - avp

4 answers 4

Just copy the array into a temporary variable a la template

 void InsertionSort(int A[], int N) { // Вот здесь: int template[A.Length]; for(int i = 0;i < A.Length;i++) { template[i] = A[i]; } int j; for (int i = 1; i < N; i++) { j = i; while (j > 0 && template[j - 1] > template[j]) { Swap(&template[j - 1], &template[j]); j--; } } //Передаем наш массив PrintA(template, N); } 
  • What language is this? - AnT
  • I liked your decision, but I only need the array not to change in the main function, but only in the InsertionSort function. By changing your code a little, I was able to achieve this. The only problem is that I cannot create a new array in the InsertionSort function, typing the constant N in square brackets, the compiler gives an error, and with your A.Lenght function as well. - Maksud3
  • Yes, I corrected. Here and the problem of copy-paste code was revealed Xd @ Maksud3 - TEA
  • @TEA I was able to solve the problem with creating a prototype array in the Insertion Point function. Allocated memory for the array through new, and your code is working. Only I renamed the array to another, because template is a reserved name. - Maksud3
  • Same question as AnT. What A.Length ? - HolyBlackCat

can memcpy?

 int main(void) { const int N = 10; int A[N], B[N]; ....код наполняющий массив A memcpy((void*)B, (void*)A, (sizeof(int) * N)); PrintA(B, N); 
  • A good option, take note. - Maksud3

Just need to copy the array - either by yourself or by a built-in function. But there is one workaround - if the array is built into the structure, then it is copied by the compiler.

 typedef struct Massiv_ { int A[100]; } Massiv; void Sort(Massiv M, int N) { // Сортируем MA - тот массив, что передан, тронут не будет } 
  • in this case, the array MA will be sorted, and it will be changed, the object reference is transferred, in this example, M, so you still need to copy A [] into MA in any case. - NewView
  • The structure simply creates a container for the array, in this case. This approach is convenient to use if there is a lot of data and it is necessary to combine them into a single whole for transport by methods and functions. - NewView
  • one
    @NewView What is the link? The structure is passed by value, i.e. the array inside it is copied. Proof - HolyBlackCat
  • @HolyBlackCat, that's right, but I specifically looked at this code, it conforms to the c99 standard, although the c ++ headers are indicated, but this is discussed in this topic. Here is an example of how the original code works . And here is an example of the same code with modifications compiled as std = c99. The results are identical, the array in the structure is passed as a link. - NewView
  • @NewView You have no structures in your code. Nobody argues with the fact that arrays are passed to a function by reference (if they are outside structures), but why do you think that it will also be passed by reference inside the structure? In addition, the results that you have on the links, you get on any version of the standard. - HolyBlackCat

Make a copy of the array inside the function and print there

 void InsertionSort(int A_tmp[], int N) { int A[sizeof(A_tmp)/sizeof(int)]; for (int i=0;i < (sizeof(A_tmp)/sizeof(int)); i++) { A[i] = A_tmp[i]; } int j; for (int i = 1; i < N; i++) { j = i; while (j > 0 && A[j - 1] > A[j]) { Swap(&A[j - 1], &A[j]); j--; } } PrintA(A, N); } 
  • @ Maksud3 - the question why you need it, you can simply return the value of the new (sorted) array and print it in the main - Fedor Lapshin