Why is my vector ordered if I am ordering in a function (void) without any return?

Asked

Viewed 40 times

-3

When sorting a vector, in a function of type {void} why the vector (original) in my function call is shown as ordered since the vector that was ordered was the vector referring to the formal parameters of my function Sort(int Vector[], int TAM), because the ordered vector should be outside the memory scope of my vector main, since both arrangements are local variables, and the vector should be displayed Values[] is not ordered.

**void OrdenacaoBolha(int Vetor[], int TAM) {
    int aux;
    for(int i=0; i<TAM; i++) {
        for(int j=0; j<TAM-1; j++) {
            if (Vetor[j]>Vetor[j+1]) {
                aux = Vetor[j];
                Vetor[j] = Vetor[j+1];
                Vetor[j+1] = aux;
            }
        }
    }
}

int main(void) {
    int Valores[] = {14, 32, 1, 7, 3, 2, 0};
    int TAM = sizeof(Valores)/sizeof(int);
    OrdenacaoBolha(Valores, TAM);
    for(int i=0; i<7; i++) {
        printf("%d\n", Valores[i]);
    }
    return 0;
}**

2 answers

1

C-language arrays are treated as pointers. This means that a variable of the array type "points" to the first element of the array. In this case, when you pass an array per parameter you are actually passing the "reference" to where the array starts, not a copy of it, so it doesn’t matter if it is the function that was declared or another, if it is changed, the change is made in the "original" array".

I don’t think that’s the problem in your example, just the doubt, but if you had to keep the original array for some reason, you would need to make a copy of it, for example by declaring another and passing each value from one array to the other in the for for example, or still using the memcopy, thus:

int Valores2[7];
memcpy(Valores2, Valores, sizeof(Valores));
OrdenacaoBolha(Valores2, TAM);

Thus passes the reference to another array, preserving the first

  • Ricardo, then in my second function I work nothing more, nothing less than just the references of each position of my original vector being a pointer. It must be why it is called passage by reference right?

  • Yes exact, you pass the reference, the object is one in memory :)

0


arrays are containers where each space is a pointer to each value so when placing array in an array parameter you end up changing the values of the original

  • then it means that each position of a vector in reality is an implicit pointer, so when I pass the argument to the function and it sorts the values is updated the original vector by pointing to that of the function. So technically the arrangements (One-dimensional, two-dimensional and three-dimensional) in a general way are only pointers.

  • in a more technical way when referring to the array only by the name of the variable it returns the address of the first element of the array, the array itself is just a way to create a structure in memory explaining in an easier way to understand: when you put "Values" as the argument of the function it returned an address where the first value 14 was stored in memory, for example in the 38AD address as every int has 4 bytes, in the function if placed Vector[1] it will add 38AD + 4 and will give the address where the second element of the array is stored in memory

Browser other questions tagged

You are not signed in. Login or sign up in order to post.