在C中复制3D数组的函数? [英] Function that copies a 3d array in C?

查看:110
本文介绍了在C中复制3D数组的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的教科书中偶然发现一个问题:写一个函数,该函数可以复制3D整数数组的内容.该功能应支持任何3D数组大小.'

Hi I stumbled upon a question in my textbook that states: 'Write a function that makes a copy of the contents of a 3D array of integers. The function should support any 3D array size.'

在与我的讲师讨论之后,他指定该函数的原型应类似于此(这是2D,我需要3D).

After discussing it with my lecturer he specified that the prototype of the function should look something similar to this (this is 2D, I need 3D).

int sum2d(int rows, int cols, int ar[rows][cols]);

现在,通过在main函数中制作所有内容,我目前的编码方式是这样的,它应按应有的方式工作,即复制所有内容等.

Now how I currently coded it is by making everything in the main function and it works like it should, i.e copies all contents etc.

 int main()
    {
        int x,y,z;
        printf("Enter x value.\n");
        scanf("%d", &x);
        printf("Enter y value.\n");
        scanf("%d", &y);
        printf("Enter z value.\n");
        scanf("%d", &z);

        int *arrx = malloc(x * sizeof(*arrx));
        int *arry = malloc(y * sizeof(*arry));
        int *arrz = malloc(z * sizeof(*arrz));



        printf("The size of the array is %d.\n", x*y*z);
        /* 3D array declaration*/
        int disp[x][y][z];
        int cpydisp[x][y][z];


        /*Counter variables for the loop*/
        int i, j, k;
        for(i=0; i<x; i++) {
            for(j=0;j<y;j++) {
                for (k = 0; k < z; k++) {
                    printf("Enter value for disp[%d][%d][%d]:", i, j, k);
                    scanf("%d", &disp[i][j][k]);
                }
            }
        }

        memcpy(cpydisp,disp, sizeof(disp));

        //Displaying array elements
        printf("Three Dimensional array elements:\n");
        for(i=0; i<x; i++) {
            for(j=0;j<y;j++) {
                for (k = 0; k < z; k++) {
                    printf("%d ", cpydisp[i][j][k]);
                }
                printf("\n");
            }
        }
    }

但是,这是不正确的,因为我需要实现仅用于复制的功能,所以我想到了这一点.创建一个名为void array_copy的函数,该函数实际上是通过memcpy将数组disp的内容复制到另一个数组cpydisp的.然后在主函数中调用功能array_copy,但这不起作用.

However this is not correct since I need to implement a function just for copying and I came up with this. Create a function called void array_copy which practically copies the contents of the array disp to another array cpydisp by memcpy. The function array_copy is then called in the main however this is not working.

       int i, j, k;
        int x,y,z;
        int disp[x][y][z];
        int cpydisp[x][y][z];


void array_copy() {

        memcpy(cpydisp, disp, sizeof(disp));

    //Displaying array elements
        printf("Three Dimensional array elements:\n");
        for (i = 0; i < x; i++) {
            for (j = 0; j < y; j++) {
                for (k = 0; k < z; k++) {
                    printf("%d ", cpydisp[i][j][k]);
                }
                printf("\n");
            }
        }
    }

    int main()
    {
        printf("Enter x value.\n");
        scanf("%d", &x);
        printf("Enter y value.\n");
        scanf("%d", &y);
        printf("Enter z value.\n");
        scanf("%d", &z);

        //int *arrx = malloc(x * sizeof(*arrx));
        //int *arry = malloc(y * sizeof(*arry));
        //int *arrz = malloc(z * sizeof(*arrz));



        printf("The size of the array is %d.\n", x*y*z);
        /* 3D array declaration*/

        /*Counter variables for the loop*/
        int i, j, k;
        for(i=0; i<x; i++) {
            for(j=0;j<y;j++) {
                for (k = 0; k < z; k++) {
                    printf("Enter value for disp[%d][%d][%d]:", i, j, k);
                    scanf("%d", &disp[i][j][k]);
                }
            }
        }

        array_copy();

    }

任何人都想知道如何修复它,因为当用户在开始任何操作之前先估算出想要的尺寸时,我似乎无法理解它到底有什么问题.

Any thoughts please on how I can get it fixed, since I can't seem to understand what's wrong with it when it clearly can take any size by the user imputing the size he wants prior to having anything started.

预先感谢

#include<stdio.h>
#include <string.h>
int x,y,z;
int i, j, k;

void user_input(){

    printf("Enter x value.\n");
    scanf("%d", &x);
    printf("Enter y value.\n");
    scanf("%d", &y);
    printf("Enter z value.\n");
    scanf("%d", &z);
}


void array_copy() {

    int disp[x][y][z];
    int cpydisp[x][y][z];

    memcpy(cpydisp, disp, sizeof(disp));
    //Displaying array elements
    printf("Three Dimensional array elements:\n");
    for (i = 0; i < x; i++) {
        for (j = 0; j < y; j++) {
            for (k = 0; k < z; k++) {
                printf("%d ", cpydisp[i][j][k]);
            }
            printf("\n");
        }
    }
}


int main()
{
    user_input();
    int disp[x][y][z];
    int cpydisp[x][y][z];

    printf("The size of the array is %d.\n", x*y*z);
    /* 3D array declaration*/

    /*Counter variables for the loop*/
    for(i=0; i<x; i++) {
        for(j=0;j<y;j++) {
            for (k = 0; k < z; k++) {
                printf("Enter value for disp[%d][%d][%d]:", i, j, k);
                scanf("%d", &disp[i][j][k]);
            }
        }
    }

    array_copy();

}

我已经以这种方式重试了,现在正在输出某些内容,而不是第二次尝试,但是输出只是随机数.即:

I have retried it this way now something is being outputted rather than the second attempt however the output is just random numbers. i.e:

Enter x value.
1

Enter y value.
2

Enter z value.
3

The size of the array is 6.

Enter value for disp[0][0][0]:1
1
Enter value for disp[0][0][1]:2
2
Enter value for disp[0][0][2]:3
3
Enter value for disp[0][1][0]:4
4
Enter value for disp[0][1][1]:5
5
Enter value for disp[0][1][2]:6
6
Three Dimensional array elements:
797168 0 6421264
0 3 0

Process finished with exit code 0

正确的输出应为1,2,3,4,5,6

再次感谢

推荐答案

鉴于您有一个实际的3D数组,该函数很简单:

Given that you have an actual 3D array, the function is simply:

memcpy( dst, src, sizeof(int[x][y][z]) ); 

如果出于学习目的,您想自己编写效率较低的版本,则如下所示:

If you want to write a less efficient version yourself, for learning purposes, then it goes like this:

void copy (size_t x, size_t y, size_t z, int dst[x][y][z], int src[x][y][z])
{
  for(size_t i=0; i<x; i++)
    for(size_t j=0; j<y; j++)
      for(size_t k=0; k<z; k++)
        dst[i][j][k] = src[i][j][k];
}

代码不起作用的原因是因为您使用了可变长度数组(VLA),但在文件范围(全局")中声明了它们,这是不允许的.必须在本地范围内声明VLA,并且必须在声明VLA之前将数组边界设置为有效值.

The reason why your code doesn't work is because you used variable-length arrays (VLA) but declared them at file scope ("global"), which isn't allowed. VLA must be declared at local scope, and the array bounds must be set to valid values before the VLA declaration.

上面的代码不在乎3D数组的分配方式,而是将其留给调用者.

The above code doesn't care how the 3D array is allocated, but leaves that to the caller.

这篇关于在C中复制3D数组的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆