C语言 在 C 中创建一个 int 数组数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5243834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:00:17  来源:igfitidea点击:

Creating an array of int arrays in C?

carraysmultidimensional-array

提问by Eric Brotto

Let us say I have the following method prototype:

假设我有以下方法原型:

void mix_audio(int *vocal_data_array, int *instrumental_data_array, int *mixed_audio_array, FOURTH ARGUMENT)
{


}

How would I:

我将如何:

  1. Initialize an array_of_arrays before the above argument so as to pass it as the fourth argument?

  2. In the method, make it so that the first value of my array_of_arrays is the array called vocal_data, that the second value of my array is instrumental_data_array and the third value is mixed_audio_array.

  3. How would I later then loop through all the values of the first array within the array_of_arrays.

  1. 在上述参数之前初始化一个 array_of_arrays 以便将它作为第四个参数传递?

  2. 在该方法中,使我的array_of_arrays 的第一个值是称为vocal_data 的数组,我的数组的第二个值是instrumental_data_array,第三个值是mixed_audio_array。

  3. 稍后我将如何遍历 array_of_arrays 中第一个数组的所有值。

I hope I'm not asking too much here. I just thought it would be simple syntax that someone could spit out pretty quickly :)

我希望我不要在这里要求太多。我只是认为这是一个简单的语法,有人可以很快吐出来:)

Thanks!

谢谢!



EDIT 1

编辑 1

Please note that although I've showed by my example an array_of_arrays of length 3 I'm actually looking to create something that could contain a variable length of arrays.

请注意,虽然我已经通过我的示例展示了一个长度为 3 的 array_of_arrays,但我实际上是想创建一些可以包含可变长度数组的东西。

回答by jmq

Simple array of arrays and a function showing how to pass it. I just added fake values to the arrays to show that something was passed to the function and that I could print it back out. The size of the array, 3, is just arbitrary and can be changed to whatever sizing you want. Each array can be of a different size (known as a jagged array). It shows your three criteria:

简单的数组数组和一个显示如何传递它的函数。我只是在数组中添加了假值,以显示某些内容已传递给函数,并且我可以将其打印出来。数组的大小 3 是任意的,可以更改为您想要的任何大小。每个数组可以有不同的大小(称为锯齿状数组)。它显示了您的三个标准:

Initialization, Assigning values to each index of arrayOfArrays, The function demonstrates how to extract the data from the array of arrays

初始化,为 的每个索引赋值arrayOfArrays,函数演示如何从数组中提取数据

#include <stdio.h>

void mix_audio(int *arr[3]);

int main() {
  int *arrayOfArrays[3];

  int vocal[3] = {1,2,3};
  int instrumental[3] = {4,5,6};
  int mixed_audio[3] = {7,8,9};

  arrayOfArrays[0] = vocal;
  arrayOfArrays[1] = instrumental;
  arrayOfArrays[2] = mixed_audio;

  mix_audio(arrayOfArrays);

  return(0);
}

void mix_audio(int *arr[3]) {
  int i;
  int *vocal = arr[0];
  int *instrumental = arr[1];
  int *mixed_audio = arr[2];

  for (i=0; i<3; i++) {
    printf("vocal        = %d\n", vocal[i]);
  }
  for (i=0; i<3; i++) {
    printf("instrumental = %d\n", instrumental[i]);
  }
  for (i=0; i<3; i++) {
    printf("mixed_audio  = %d\n", mixed_audio[i]);
  }
}

回答by GrahamS

From your question it sounds like you actually want a structcontaining your arrays, something like:

从您的问题听起来,您实际上想要一个struct包含您的数组,例如:

struct AudioData {

    int* vocal_data_array;
    unsigned int vocal_data_length;

    int* instrumental_data_array;
    unsigned int instrumental_data_length;

    int* mixed_audio_array;
    unsigned int mixed_audio_length;
};

回答by hennes

For the array allocation using the example of an array of integers:

对于使用整数数组示例的数组分配:

int** x = malloc (sizeof (int*) * rows);
if (! x) {
    // Error
}

for (int i = 0; i < rows; ++i) {
    x[i] = malloc (sizeof (int) * columns);
    if (! x[i]) {
        // Error
    }
}