6

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.

1
  • 1
    Wouldn't it be redundant to have parameters for all 3 single arrays and also a fourth array, which contains the former 3? Commented Mar 9, 2011 at 9:53

3 Answers 3

12

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]);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

3

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;
};

Comments

1

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
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.