-1

I've been working in C lately and I've started working with arrays recently. Can you help me identify what's causing the issue here? I'm fairly certain the function inputArray is, but I cannot figure out why.

#include <stdio.h>

void inputArray(int array[], int size);

int main() {
    int arraySize;
    printf("Enter size of array: \n");
    scanf("%d", &arraySize);
    int myArray[arraySize];
    inputArray(myArray[arraySize], arraySize);

    return 0;
}


void inputArray(int array[], int size){
    int i;
    for (i=0; i<size; i++){
        printf("Enter value for array: \n");
        scanf("%d", array[i]);
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
emcosokic
  • 51
  • 6
  • 2
    Even with standard settings, the compiler gives you warnings that explains the problem well. Warnings are there to help you. Read them. – klutt Sep 09 '22 at 16:30
  • foo(type bar) cannot change 'bar' because arguments are COPIED into parameters. Any call that must change variables in the caller, (eg. scanf), must therefore be provided with address arguments. – Martin James Sep 09 '22 at 16:30

2 Answers2

3

This call

inputArray(myArray[arraySize], arraySize);

is incorrect. The compiler shall issue a message that the passed first argument has no the type int *. You are passing a non-existent element of the array with the index arraySize.

You need to write

inputArray(myArray, arraySize);

Also you have to write

scanf("%d", &array[i]);

or

scanf("%d", array + i);

instead of

scanf("%d", array[i]);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

You do not need to pass in myarray[arraysize] into your helper function. Just myarray

freyfrey01
  • 53
  • 7