Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an array of int arrays in C?

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.

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

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.

like image 473
Eric Brotto Avatar asked Mar 09 '11 09:03

Eric Brotto


2 Answers

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:

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

#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]);
  }
}
like image 103
jmq Avatar answered Oct 05 '22 15:10

jmq


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

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;
};
like image 42
GrahamS Avatar answered Oct 05 '22 15:10

GrahamS