0

I want to pass an array of three structures to a function that sorts the three numbers stored in the array of structures, but do not want to change the actual values ​​, as arrays are passed by reference , there's my problem , any ideas accept thanks :)

Here is my code for review:

#include <stdio.h>
#define N 3
typedef struct
{
    int digito;
} numbers;

void  sort(numbers tabla[], int nro);
void enternums(numbers  tabla[], int nro);

int main()
{
    static numbers enter[N];
    int i, j, tmp;
    enternums(enter, N);
    printf("Data before entering the function...\n\n");
    for (i = 0 ; i < N ; i++)
    {
        printf("%d\n", enter[i].digito);
    }
    sort(enter, N);

    printf("After entering data function....\n\n");
    for (i = 0 ; i < N ; i++)
    {
        printf("%d\n", enter[i].digito);
    }
    return 0;
}

void enternums(numbers tabla[], int nro)
{
    int i;
    for (i = 0 ; i < nro ; i++)
    {
        printf("Enter a digit for the position %d:  ", i);
        scanf("%d", &tabla[i].digito);
    }
}

void sort(numbers tabla[N], int nro)
{
    int i, j, tmp;
    for (i = 1 ; i < nro ; i++)
    {
        j = i;
        tmp = tabla[i].digito;
        while ((j > 0) && (tmp < tabla[j - 1].digito))
        {
            tabla[j].digito = tabla[j - 1].digito;
            j--;
        }
        tabla[j].digito = tmp;
    }
}
5
  • 1
    You don't need to write the code in such a way that it's so hard to read, you can do it like I did. And certainly arrays are not passed by reference. Commented Oct 18, 2015 at 3:48
  • I don't really understand what you actually want, and scanf("%d", &tabla[i].digito); is a potential bug in your code since you don't check for the returned value, i.e. success or failure of scanf(). Commented Oct 18, 2015 at 3:54
  • 1
    You could make a copy of the passed array and sort the copy Commented Oct 18, 2015 at 3:54
  • @bpgeck Which would make sens if you return the array, otherwise it's just an intentional memory leak! Commented Oct 18, 2015 at 3:54
  • @iharob He doesn't have to do a deep copy. He can just store it on the stack. Commented Oct 18, 2015 at 3:55

2 Answers 2

1

Passing the array as a pointer1 to it's first element, has the advantage that you can actually pass another array to store the new values

void sort(const numbers *const tabla, numbers *resultado, int nro)
{
    int i, j, tmp;
    for (i = 1 ; i < nro ; i++)
        resultado[i].digito = tabla[i].digito;
    for (i = 1 ; i < nro ; i++)
    {
        tmp = resultado[i].digito;
        for (j = i ; (j > 0) && (tmp < resultado[j - 1].digito) ; --j)
            resultado[j].digito = resultado[j - 1].digito;
        resultado[j].digito = tmp;
    }
}

Making the while loop a for loop instead, it would make your code clearer.

And to use this

int main()
{
    static numbers enter[N];
    static numbers ordenado[N];
    int i, j, tmp;
    enternums(enter, N);
    printf("Data before entering the function...\n\n");
    for (i = 0 ; i < N ; i++)
    {
        printf("%d\n", enter[i].digito);
    }
    sort(enter, ordenado, N);

    printf("After entering data function....\n\n");
    for (i = 0 ; i < N ; i++)
    {
        printf("%d\n", ordenado[i].digito);
    }
    return 0;
}

Also, make your code safer by checking for possible errors, for example

scanf("%d", &tabla[i].digito);

should be

if (scanf("%d", &tabla[i].digito) != 1)
    Oops_there_is_a_problem_dont_use_this_value_maybe_ask_again();

1Which happens automatically, it's not passed by reference it's converted to a pointer.

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

Comments

0

You cannot pass arrays by value unless they are contained in a struct.

You can use:

typedef struct MyStrutArray
{
   numbers  enter[N];
} MyStructArray;

and then use:

void  sort(MyStructArray array);

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.