[英]Passing array of structures to function by value in C
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;
}
}
Passing the array as a pointer 1 to it's first element, has the advantage that you can actually pass another array to store the new values 将数组作为第一个元素的指针1传递,其优点是您实际上可以传递另一个数组来存储新值
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. 将
while
循环改为for
循环,可以使代码更清晰。
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();
1 Which happens automatically, it's not passed by reference it's converted to a pointer . 1 这是自动发生的,不会通过引用传递,而是转换为指针 。
You cannot pass arrays by value unless they are contained in a struct
. 您不能按值传递数组,除非它们包含在
struct
。
You can use: 您可以使用:
typedef struct MyStrutArray
{
numbers enter[N];
} MyStructArray;
and then use: 然后使用:
void sort(MyStructArray array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.