在C中,是否无法使返回类型成为数组?我开始学习操作系统课程中的指针,我需要创建一个函数,它将2个数组作为参数,并返回一个只包含两个参数数组中的元素的数组。
这是我到目前为止我的C函数返回一个数组:
#include <stdio.h>
main()
{
printf("Hello world");
int array1[4] = {1, 2, 3, 4};
int array2[4] = {3, 4, 5, 6};
int* inter = intersection(array1, array2);
printf(inter); // <-- also, I don't know how I could get this to work for testing
//freezes program so it doesn't terminate immediately upon running:
getchar();
}
int* intersection(int array1[], int array2[])
{
int arrayReturn[sizeof(array1) + sizeof(array2)];
int count = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(array1[i]==array2[j])
{
arrayReturn[count] = array1[i];
count = count + 1;
}
}
}
return arrayReturn;
}
我的另一个问题是如何使用printf()语句在main()方法中测试此函数?
我之所以需要这样做是因为我们正在学习进程和内存分配,而指针在OS开发中起着重要作用。我的教授告诉我,指针很难理解,他们会从许多编程语言中留下指针。
答案 0 :(得分:1)
我们走了
#include <stdio.h>
/* declare function, you need to capture array size as well, and also allow
to get result target pointer, result space info and pointer to place where the
size of result will be captured */
int* intersection(int * array1, int a1len , int * array2, int a2len, int* result, int *resultsize, int resultspace);
main()
{
printf("Hello world\n");
int array1[4] = {1, 2, 3, 4};
int array2[4] = {3, 4, 5, 6};
int arrayr[32]; /*result will be in this table */
int resultsize;
int resultspace = 32;
int i;
/* here comes confusion - int resultsize means it will be read as integer,
so we need to write &resultsize to get the pointer,
array declaration like int arrayr[number] is actually understood by system
as a pointer to an integer, pointing to first element of array,
allowing you to use indexes
so arrayr[3] actually means *(arrayr + 3 * sizeof(int))
*/
int* inter = intersection(array1, 4, array2, 4, arrayr, &resultsize, resultspace);
/* inter is now a pointer to arrayr */
for (i = 0; i<resultsize; i=i+1) {
printf("%d\n", inter[i]);
}
//freezes program so it doesn't terminate immediately upon running:
getchar();
}
int* intersection(int * array1, int a1len , int * array2, int a2len, int* result, int *resultsize, int resultspace)
{
/* as we received a pointer to resultsize (*resultsize)
we need to de-reference it using "*" to get or set the actual value */
*resultsize = 0;
int i, j;
for(i = 0; i < a1len; i++)
{
for(j = 0; j < a2len; j++)
{
if(array1[i]==array2[j])
{
result[*resultsize] = array1[i];
*resultsize = *resultsize + 1;
if (resultspace == *resultsize)
return result;
}
}
}
return result;
}
答案 1 :(得分:1)
在C中,是否无法使返回类型成为数组?
没有。区分数组和指针的一些特性是:
sizeof array
的计算结果为n * sizeof *array
,其中n是元素的数量,而不是指针的大小。&array
计算指向数组的指针,而不是指向指针的指针。int *ptr = (int[]){ 1, 2, 3, 4 };
,但你不能使用指针初始化数组,例如。 int array[4] = ptr;
。int array[4]; array = (int[]){ 1, 2, 3, 4 };
,但您可以指定一个指针:int *ptr; ptr = (int[]){ 1, 2, 3, 4 };
,除非指针被声明为指向int的const指针,例如。 int * const ptr = NULL; ptr = (int[]){ 1, 2, 3, 4 };
我开始学习操作系统课程中的指针 我需要创建一个以2个数组作为参数的函数 返回一个只包含两者中的元素的数组 参数数组。
这是不可能的。首先,您的数组参数实际上是指针参数。查看sizeof array1
和sizeof array2
。尝试使用它们初始化数组。尝试分配给他们。上面有多少测试似乎表明它们是指针?将数组传递给函数时,数组表达式求值为指向数组第一个元素的指针。也许你想声明你的函数接受指向数组的指针,例如:
int *intersection(size_t sz1, int (*array1)[sz1], // array1 is a pointer to int[sz1]
size_t sz2, int (*array2)[sz2]) // array2 is a pointer to int[sz2]
{ ... }
其次,你的函数清楚地返回一个指针值,不是一个数组。关于该返回值,arrayReturn在交集中被声明为具有自动存储持续时间的数组,因此在交集返回时将被销毁。当主要尝试使用该值时,它将尝试使用已销毁的数组。无法使用自动存储持续时间返回阵列。使用自动存储持续时间返回固定大小的结构是可能的,但这对您的问题没有帮助,因为您的返回值需要是动态的。
我的另一个问题是如何在中测试此功能 main()方法使用printf()语句?
您无法对该函数的返回值执行任何操作,因为使用已销毁的对象是未定义的行为。
我之所以需要这样做是因为我们正在学习 进程和内存分配和指针在操作系统中发挥着重要作用 发展。
C编程语言独立于OS实现;操作系统是否延迟了具有自动存储持续时间的对象的破坏并不重要。如果使用已销毁的对象,则会调用未定义的行为。
我的教授告诉我,指针很难理解 他们从许多编程语言中留下指针。
他/她是否写出了他/她的课程计划?如果没有,那么他/她就错过了一个可以确定改进的关键点。他/她的书面课程计划过去有多成功?如果它已经100%成功,那么使用像“困难”这样的词是没有意义的;你为什么要在学生中不必要地引发压倒性的感情?如果零件太复杂,那么识别和澄清这些零件更有意义,而不是识别那些零件并将它们指定为“困难”。在关于C的课程中提及其他编程语言也没有意义。
当教授的课程计划变得相当成功时,将其作为一本书出版是可行的。一个例子是K&amp; R的“The C Programming Language,Second Edition”。你有一本书吗?