Home > database >  How to pass the user value for the size of an array into a function implemented in another code file
How to pass the user value for the size of an array into a function implemented in another code file

Time:10-21

I have no idea what to do. Whenever I try to insert read_num[size] it does not work. My debugger shows the value as 0. I want to take a value for the size of the array from the user. Then I want to use the value in another file.

So what I want to do is, I will do a printf to ask the user to give a value so that I can pass it as the size of the array. I did and it didn't work. Then I decided to write a direct value [36]. But I want user to insert 36.

This is what I have tried:

main.c

int main(void)
{
    int numbers[ARR_SIZE] = { 0 };
    int size = 36; // I am doing directly but I want to take the value from the user but it just does not work. 
    double mean = 0;
    double stddev = 0;
   
    read_array(size);
}

file.c

int read_array(int a[36]) //placing 36 directly, I want it to be placed by the user
{
    int num_read[36]; int i = 0; // I have no clue whats i am doing. I just want to pass the value  from the user. 
    while (i < a)
    {
   
        printf("Enter number");
        scanf_s("%d", &num_read[i]);
          i; 
     
    }
}

header file

#include <stdio.h>
#include <math.h>
#define ARR_SIZE 100
int read_array(int arr[ARR_SIZE]);
double calc_mean(int arr[], int size);
double calc_stddev(int arr[], int size);
void print_array(int arr[], int size);

CodePudding user response:

In you main.c file you want to decide the value of size and use that value in a call to the function read_array().
You do so by calling read_array(size);, which makes sense to me.

Sadly your shown code lacks the details of whether or not you include your header file (and others). I assume that it does.

However, if what you are doing inside main() is actually what you want to do (I assume so), then the header file and the implementation of read_array() does not match that intention.

This int read_array(int arr[ARR_SIZE]), in your header and your implementation, means
"Dear compiler, this function takes an array (or a pointer to 100 consecutive ints)."

That does not match your plan of "I will give a single int as the parameter to the function."
To match that plan, use a prototype of int read_array(int a);.
The implementation code as shown should then work.

If you want the local array to actually be of the size given by the user and arriving via the parameter a, then change
int num_read[36]; to int num_read[a];.

However, I suspect that you then intend to return the array which you filled with input from the user.
That is neither possible with a prototype of int read_array(int a); nor with one of int read_array(int arr[ARR_SIZE]);. Both mean "This function will return a single int.".
I assume you will need help with that, but you need to ask a separate question on how to return a new array from a C function - or better first search StackOverflow for a duplicate.

CodePudding user response:

  1. To pass the size of the array to the function (it does not matter if it is defined in the same or another compilation units) you need to have an additional parameter to the function. There is no other way of doing it in C language
int *doSomethingWithArray(int *array, size_t size)
{
    /* ... */    
}

But I want user to insert 36.

The user can only enter something using I/O functions and has no access to the C code.

int *initArray(int *array, const size_t size)
{
    for(size_t index = 0; index < size; index   )
        array[index] = rand();
    return array;
}

int *printArray(int *array, const size_t size)
{
    for(size_t index = 0; index < size; index   )
        printf("array[%3zu] = %d\n", index, array[index]);
    return array;
}

int main(void)
{
    size_t array_size;

    srand(time(NULL));
    if(scanf("%zu", &array_size) == 1)  // user enters the size of the array
    {
        int array[array_size];

        initArray(array, array_size);
        printArray(array, array_size);
    }
}

https://godbolt.org/z/xjh9qGcrz

  • Related