Filling array in C by using functions
arrays, c, function, pointers
Solution
Assuming that `input_data` should set all array values to a known value, you could write
void input_data(int *data, int size, int value){
for (int i=0; i<size; i++) {
data[i] = value;
}
}
calling this like
input_data(*data1, *size1, 5); // set all elements of data1 to 5
The key point here is that you can use `(*data1)[index]` to access a particular array element and can pass your arrays as `int*` arguments.
Problem
Okay, so I am calling function fill_arrays like this: ``` fill_arrays(&data1, &data2, &size1, &size2); ``` fill_arrays looks like this: ``` void fill_arrays(int **data1, int **data2, int *size1, int *size2){ *size1 = get_size(*size1, 1); *size2 = get_size(*size2, 2); *data1 = malloc(*size1 * sizeof(int *)); *data2 = malloc(*size2 * sizeof(int *)); input_data(&data1, *size1, 1); } ``` In input_data function I would like to assign some numbers to an array: ``` void input_data(int **data, int size, int index){ *data[5] = 5; } ``` The problem is, I am completely lost with pointers... Maybe you can tell me how should I call function input_data in order to be able to assign some numbers to data array?