繁体   English   中英

返回数组的函数(用 C 编写的 Win32 控制台应用程序,学习操作系统)

[英]function that returns an array (Win32 console application written in C, Learning about operating systems)

在 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() 方法中测试此函数?

我需要这样做的原因是因为我们正在学习进程和内存分配,并且指针在操作系统开发中起着重要作用。 我的教授告诉我,指针很难理解,以至于许多编程语言都没有使用指针。

开始了

#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;
}

在 C 中,不能将返回类型设为数组吗?

不。将数组与指针区分开来的一些特征是:

  1. sizeof array计算结果为n * sizeof *array ,其中 n 是元素的数量,而不是指针的大小。
  2. &array计算为指向数组的指针,而不是指向指针的指针。
  3. 您可以使用数组来初始化指针,例如。 int *ptr = (int[]){ 1, 2, 3, 4 }; ,但您不能使用指针来初始化数组,例如。 int array[4] = ptr; .
  4. 您不能分配给数组,例如。 int array[4]; array = (int[]){ 1, 2, 3, 4 }; ,但您可以分配给指针: int *ptr; ptr = (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 array1sizeof 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]
{ ... }

其次,您的函数清楚地返回一个指针值而不是一个数组。 关于该返回值, arrayReturnintersection内被声明为具有自动存储持续时间的数组,因此当intersection返回时它将被销毁。 main尝试使用该值时,它将尝试使用已破坏的数组。 无法使用自动存储持续时间返回数组。 使用自动存储持续时间返回固定大小的struct是可能的,但这对您的问题没有帮助,因为您的返回值需要在大小上是动态的。

我的另一个问题是如何使用 printf() 语句在 main() 方法中测试此函数?

您不能对该函数的返回值做任何事情,因为使用已销毁的对象是未定义的行为。

我需要这样做的原因是因为我们正在学习进程和内存分配,并且指针在操作系统开发中起着重要作用。

C 编程语言独立于操作系统实现; 操作系统是否会延迟销毁具有自动存储持续时间的对象并不重要。 如果您使用被破坏的对象,您将调用未定义的行为。

我的教授告诉我,指针很难理解,以至于许多编程语言都没有使用指针。

他/她有没有写出他/她的课程计划? 如果不是,那么他/她错过了一个可以确定改进的关键点。 他/她的书面教案过去有多成功? 如果它是 100% 成功的,那么使用像“困难”这样的词是没有意义的; 为什么要不必要地引发学生的强烈情绪? 如果部分太复杂,那么识别和澄清这些部分更有意义,而不是识别这些部分并将它们指定为“困难”。 在有关 C 的课程中​​提及其他编程语言也是没有意义的。

当教授的课程计划相当成功时,将其作为一本书出版变得可行。 一个例子是 K&R 的“The C Programming Language, Second Edition”。 你有书吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM