4

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?

4
  • 1
    What is the overall objective? What is the program supposed to do? Commented Apr 16, 2013 at 15:38
  • 2
    you need to replace sizeof(int * ) by sizeof(int) and pass data1 to input_data instead of the reference &data. Commented Apr 16, 2013 at 15:40
  • Which book are you reading? Commented Apr 16, 2013 at 15:50
  • Is the intended goal of the function fill_arrays() to (a) determine the sizes of the arrays being filled, (b) allocate space for arrays size, (c) fill the allocated arrays, and (d) return as out-parameters the arrays and the resultant sizes of each? That is what the prototype of fill_arrays() looks like you want or are hoping for. Is that the case? Commented Apr 16, 2013 at 15:58

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If I call the function using the array with * This gives me error: "argument makes pointer from integer without a cast [-Wint-conversion]". Instead, I call the function like this: input_data(data1, *size1, 5);
5

I stumbled upon this question while doing a homework assignment and the answer doesn't strike me as entirely satisfactory, so I'll try to improve upon it.

Here is a small program that will establish an array of a user-defined size, fill it arbitrarily, and print it.

#include <stdio.h>
#include <stdlib.h>

void fill_array(int *array, int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        // fill array like [1, 2, 3, 4...]
        array[i] = i+1;
    }
}

void print_array(int *array, int n)
{
    int i;
    printf("[");
    for (i = 0; i < n; i++)
    {
        if (i == (n-1))
            printf("%d]\n", array[i]);
        else
            printf("%d, ", array[i]);
    }
}

int main()
{
    int n;
    printf("Please enter a size for your array>");
    scanf("%d", &n);

    // dynamically allocate memory for integer array of size n
    array = (int*) malloc (n * (sizeof(int)));

    fill_array(array, n);
    print_array(array, n);

    return 0;
}

I hope this helps anyone who is learning C for the first time, or coming back to it after years away from the tried and true language, like me.

Comments

1

It seems you should also add int *array = NULL in order to get it working.

Comments

Your Answer

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