如何打印从函数返回的数组

时间:2015-11-22 11:20:24

标签: c arrays

此代码应该接收到数组,然后调用函数将它们返回到1个数组中但是我不知道如何打印从函数返回的最后一个数组,提前感谢? ?

现在我写了任何东西,因为它说帖子主要是代码:D:D

#include <stdio.h>
#include <stdlib.h>

int join_arrays(int *array1, int *array2, int arr1_size, int arr2_size);

int main() {
    int size_arr1, size_arr2, i, num1 = 1, s;

    printf("Please enter the size of the first array: ");
    scanf("%d", &size_arr1);
    int arr1[size_arr1];

    printf("start fill your first array: \n");
    for (i = 0; i < size_arr1; i++) {
        printf("enter element number %d: ",num1);
        scanf("%d", &arr1[i]);
        num1++;
    }
    num1 = 1;

    printf("Please enter the size of the second array: ");
    scanf("%d", &size_arr2);
    int arr2[size_arr2];
    int *ptr1_last;

    printf("start fill your second array: \n");
    for (i = 0; i < size_arr2; i++) {
        printf("enter element number %d: ", num1);
        scanf("%d", &arr2[i]);
        num1++;
    }

    ptr1_last = join_arrays(arr1, arr2, size_arr1, size_arr2);

    printf("sorted array= \n");

    for (s = 0; s < (size_arr1 + size_arr2); s++) {
        printf("%d\n", ptr1_last);
    }

    return 0;
}

int join_arrays(int *array1, int *array2, int arr1_size, int arr2_size) {
    int counter_arr1, counter_arr2, m = 0;
    int last_arr[arr1_size + arr2_size];

    for (counter_arr1 = 0; counter_arr1 < arr1_size; counter_arr1++) {
        last_arr[counter_arr1]=array1[counter_arr1];
    }
    for (counter_arr2 = counter_arr1; counter_arr2 < (arr1_size + arr2_size); counter_arr2++) {
        last_arr[counter_arr2] = array2[m];
        m++;
    }

    return last_arr[0];
}

2 个答案:

答案 0 :(得分:1)

修改代码以在main中创建接收数组并将指针传递给merge函数,因为当代码中返回的函数时,本地数组last_arr将不再存在。

#include <stdio.h>
#include <stdlib.h>

//Prototype changed to include a pointer to the receiving array, also no longer returns a value.
void join_arrays(int *last_arr, int *array1,int *array2,int arr1_size,int arr2_size);

int main()
{
    int size_arr1,size_arr2,i,num1=1,s;

    printf("Please enter the size of the first array: ");
    scanf("%d",&size_arr1);
    int arr1[size_arr1];

    printf("start fill your first array: \n");
    for(i=0; i<size_arr1; i++)
    {
        printf("enter element number %d: ",num1);
        scanf("%d",&arr1[i]);
        num1++;
    }
    num1=1;

    printf("Please enter the size of the second array: ");
    scanf("%d",&size_arr2);
    int arr2[size_arr2];
    int *ptr1_last;

    printf("start fill your second array: \n");
    for(i=0; i<size_arr2; i++)
    {
        printf("enter element number %d: ",num1);
        scanf("%d",&arr2[i]);
        num1++;
    }

    int last_arr[size_arr1 + size_arr2];    //Create receiving array here
    join_arrays(last_arr, arr1,arr2,size_arr1,size_arr2);  //And pass it to the function.

    printf("merged array= \n");

    for(s=0;s<(size_arr1+size_arr2);s++)
    {
        printf("%d\n", last_arr[s]);
    }

    return 0;
}

void join_arrays(int *last_arr, int *array1,int *array2,int arr1_size,int arr2_size)
{
    int counter_arr1, m=0;
    for(counter_arr1=0; counter_arr1<arr1_size; counter_arr1++)
    {
        last_arr[counter_arr1]=array1[counter_arr1];
    }
    for(; counter_arr1<(arr1_size+arr2_size); counter_arr1++)
    {
        last_arr[counter_arr1]=array2[m];
        m++;
    }   
}

答案 1 :(得分:1)

使用该函数只返回last_array的第一个元素,你应该创建一个全局数组,这样它在所有函数中都可见,或者在内存中返回last_array [0]位置的指针