我被要求定义一个函数,该函数接受一个列表的值并将其元素分配到两个列表中:一个表示奇数,另一个表示偶数。我创建了函数array_oddeven()
并传递了两个参数:
int *ptr
:指向数组的指针
int length
:表示数组的大小
并在int main()
#include <stdio.h>
#include <stdlib.h>
#define UPPER_BOUND 8
#define MAX 100
#define SIZE 12
void array_print(int *ptr, int length) {
for (int i = 0; i < length; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
}
int* array_create(int length) {
int *t = (int*)malloc(length * sizeof(int));
for (int i = 0; i < length; i++) {
t[i] = rand() % MAX;
}
return t;
}
int *array_oddeven(int *ptr, int length){
int *even = (int*)malloc(length * sizeof(int));
int *odd = (int*)malloc(length * sizeof(int));
int j=0;
int k=0;
for (int i = 0; i < length; i++)
{
if (ptr[i] % 2 == 0) {
even[j] = ptr[i];
j++;
} else {
odd[k] = ptr[i];
k++;
}
}
return even, odd;
}
int main()
{
int *t = array_create(SIZE);
int *even = array_oddeven(t, SIZE);
int *odd = array_oddeven(t, SIZE);
array_print(t, SIZE);
array_print(even, SIZE);
array_print(odd, SIZE);
free(odd);
free(even);
free(t);
return 0;
}
83 86 77 15 93 35 86 92 49 21 62 27
83 77 15 93 35 49 21 27
86 86 92 62
83 86 77 15 93 35 86 92 49 21 62 27
83 77 15 93 35 49 21 27 0 0 0 0
83 77 15 93 35 49 21 27 0 0 0 0
我的错误在哪里?
答案 0 :(得分:3)
正如注释中指出的那样,您的代码没有执行您可能认为的事情:
return even, odd;
使用逗号运算符What does the comma operator , do?
事实上,它不会返回两个值,而只会返回一个。
如果要检索两个数组,则可以将返回值作为函数的输入参数进行传递;这是我的样子:
void array_oddeven(const int *ptr, int length, int **podd, int **peven){
/* fill both arrays with 0 as your print function go thru the whole
* lenght of elements */
int *even = calloc(length, sizeof(int));
int *odd = calloc(length, sizeof(int));
int j=0;
int k=0;
for (int i = 0; i < length; i++)
{
if (ptr[i] % 2 == 0) {
even[j] = ptr[i];
j++;
} else {
odd[k] = ptr[i];
k++;
}
}
*peven = even;
*podd = odd;
}
int main()
{
int *t = array_create(SIZE);
int *even;
int *odd;
array_oddeven(t, SIZE, &odd, &even);
array_print(t, SIZE);
array_print(even, SIZE);
array_print(odd, SIZE);
free(odd);
free(even);
free(t);
return 0;
}
值得一提的是,由于对偶数/奇数元素的了解甚少,因此您将返回“未知”大小的数组,因此您可能会以相同的方式传递输出的元素数:
void array_oddeven(const int *ptr, int length, int **podd, size_t *poddnumelts,
int **peven, size_t *pevennumelts) {
/* malloc is back here as the array does not require to be filled with
* zeros as we return the number of elements that were set */
int *even = malloc(length * sizeof(int));
int *odd = malloc(length * sizeof(int));
int j = 0;
int k = 0;
for (int i = 0; i < length; i++)
{
if (ptr[i] % 2 == 0) {
even[j] = ptr[i];
j++;
} else {
odd[k] = ptr[i];
k++;
}
}
*peven = even;
*pevennumelts = j;
*podd = odd;
*poddnumelts = k;
}
int main()
{
int *t = array_create(SIZE);
int *even, *odd;
size_t oddnumelts, evennumelts;
array_oddeven(t, SIZE, &odd, &oddnumelts, &even, &evennumelts);
array_print(t, SIZE);
array_print(even, evennumelts);
array_print(odd, oddnumelts);
free(odd);
free(even);
free(t);
return 0;
}
以更一般的方式,您可以认为将没有大小传递给函数的数组视为杂乱无章(在C语言中),因为以后无法“猜测”它,因此是“普通” API总是会问这个数组及其大小。
答案 1 :(得分:0)
C语言中的函数仅返回一件事,由于逗号运算符在求值后会丢弃左操作数,然后对右操作数求值并认为其结果,因此您基本上只在这里返回奇数数组。
答案 2 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#define UPPER_BOUND 8
#define MAX 100
#define SIZE 12
int counteven,countodd;
void array_print(int *ptr, int length) {
for (int i = 0; i < length; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
}
int* array_create(int length) {
int *t = (int*)malloc(length * sizeof(int));
for (int i = 0; i < length; i++) {
t[i] = rand() % MAX;
}
return t;
}
int *array_oddeven(int *ptr, int length){
int j=0;
int k=0;
for (int i = 0; i < length; i++)
{
if (ptr[i] % 2 == 0) {
counteven++;
} else {
countodd++;
}
}
int *even,*odd;
even = (int*)malloc(counteven * sizeof(int));
odd = (int*)malloc(countodd * sizeof(int));
for (int i = 0; i < length; i++)
{
if (ptr[i] % 2 == 0) {
*(even+j)=ptr[i];
j++;
} else {
*(odd+k)=ptr[i];
k++;
}
}
array_print(odd, countodd);
array_print(even, counteven);
}
int main()
{
int *t = array_create(SIZE);
int *even;
int *odd;
array_print(t, SIZE);
array_oddeven(t, SIZE);
free(odd);
free(even);
free(t);
return 0;
}
`