2013-02-21 18 views
0

在C中,是否不可能使返回类型成为数组?我开始学习操作系统课程中的指针,并且需要创建一个以2个数组为参数的函数,并返回一个只包含两个参数数组中的元素的数组。函数返回一个数组(用C语言编写的Win32控制台应用程序,了解操作系统)

这是我到目前为止有我的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; 
} 

另一个问题我已经是我怎么能在main()方法测试此函数使用printf()的声明?

我需要这样做的原因是因为我们正在学习进程和内存分配,指针在操作系统开发中扮演着重要的角色。我的教授告诉我,指针是很难理解的,他们留下许多编程语言的指针。

回答

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

作为次要的补充:当结果数组空间不足时返回''返回结果;'从'intersection()'返回是有价值的,而不是返回任何内容。 – Simon 2013-02-21 01:16:37

+0

有效点,我太快了。代码按建议更新 – 2013-02-21 01:26:00

1

在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指针,例如。 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] 
{ ... } 

其次,你的函数显然返回指针值数组。关于返回值,arrayReturn在交集内被声明为一个具有自动存储持续时间的数组,因此当交集返回时它将被销毁。当主要尝试使用该值时,它将尝试使用被破坏的数组。使用自动存储持续时间返回数组是不可能的。使用自动存储持续时间返回一个固定大小的结构是可能的,但这对您的问题没有帮助,因为您的返回值需要动态变化。

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

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

我之所以要做到这一点是因为我们正在学习 进程和内存分配和指针发挥OS 发展的一大作用。

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

我的教授告诉我指针很难理解, 他们离开许多编程语言的指针。

他/她写出了他/她的课程计划吗?如果不是,那么他/她缺少可以识别改进的关键点。他/她的写作课程计划在过去有多成功?如果成功率达到100%,那么使用“困难”这样的词就没有意义;为什么你想不必要地引发学生的压倒性的感受?如果零件太复杂,那么识别和澄清这些零件就更有意义,而不是识别这些零件并将它们指定为“困难”。在关于C的课程中提及其他编程语言也是没有意义的。

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

相关问题