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

时间:2021-12-08 19:21:19

This code is supposed to recieve to arrays and then call function to return them in 1 array but I don't know how to print the last array returned from the function thanks in advance ???

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

and now I write anything because it says that the post is mostly code :D :D

现在我写任何东西,因为它说帖子主要是代码: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 个解决方案

#1


1  

Modified the code to create the receiving array in main and pass a pointer to it to the merge function because the local array last_arr would no longer exist when the function returned in your code.

修改了代码以在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++;
    }   
}

#2


1  

With that function you return only the first element of the last_array, you should create a global array so it's visible in all functions, or return a pointer of the last_array[0] position in memory

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

#1


1  

Modified the code to create the receiving array in main and pass a pointer to it to the merge function because the local array last_arr would no longer exist when the function returned in your code.

修改了代码以在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++;
    }   
}

#2


1  

With that function you return only the first element of the last_array, you should create a global array so it's visible in all functions, or return a pointer of the last_array[0] position in memory

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