如何将 3D 字符数组传递给函数 [英] How to pass 3D array of chars to a function

查看:14
本文介绍了如何将 3D 字符数组传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 chars table[][][] 的 3D 数组,我想将它传递给一个 void 函数,以便它可以对其进行更改.我怎样才能做到这一点?

I have a 3D array of chars table[][][] and I want to pass it to a void function so it can make changes to it. How can I do this?

void make(char minor[][][]);

.....
char greater[20][30][50];
make(greater);

我想这行不通.

与此相关的另一个问题:假设我想创建一个复制函数来将字符串复制到数组中 - 我应该如何在函数中调用 strcpy?

Another question connected with this: Say I want to make a copy function to copy a string into the array - how should I call the strcpy in the function?

void copy(char (*minor)[][])

{ char m[50] = "asdasdasd"; 
strcpy(minor[][],m);
}

推荐答案

如果你想传递一个数组,就像那个数组一样,而不是你用 动态分配的数组malloc(这将是几级指针,而不是真正的数组),以下任何函数原型都可以工作:

If you wanted to pass an array, just like that one, as opposed to one you've dynamically allocated with malloc (which would be a few levels of pointers, not real arrays), any of the below function prototypes will work:

  • void make(char minor[20][30][50])
  • void make(char minor[][30][50])
  • void make(char (*minor)[30][50])

你不能有类似 void make(char ***minor) 的原因是因为 minor 只会衰减为指向数组数组的指针,这样说不会衰减不止一次.

The reason being you can't have something like void make(char ***minor) is because minor will only decay into a pointer to an array of arrays, it won't decay more than once so to say.

这篇关于如何将 3D 字符数组传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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