1

I have this code:

#include <stdio.h>

void replaceIndexElement (int index, int element);

int main () 
{
    replaceIndexElement(2, 3);
    return 0;
}

void replaceIndexElement (int index, int element) {
    int mainArr[10] = {0, 2, 1000, 6, 9, 11, 43, 8, 123, 87};
    mainArr[index] = element;
    for (int i = 0; i < 10; i++) {
        printf("%d\n", mainArr[i]);
    }
}

I need pass array mainArr to function argument to pass any array and change the element by index.

I have some errors after passing array to argument.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
emblox
  • 37
  • 5
  • 2
    What does your tutorials, books or other learning materials say about passing arrays as arguments to functions? – Some programmer dude Mar 21 '23 at 09:05
  • And what have you tried? What "errors" Are you getting? Please create a [mre] of the code that you actually have problems with, and show it to us together with a description of the problems you have (including the full and complete build-log, copy-pasted as text, if you get build errors). Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Mar 21 '23 at 09:06

1 Answers1

1

It seems you mean the following

#include <stdio.h>

int replaceIndexElement( int a[], size_t n, size_t index, int element );

int main ( void ) 
{
    int mainArr[] = { 0, 2, 1000, 6, 9, 11, 43, 8, 123, 87 };
    const size_t N = sizeof( mainArr ) / sizeof( *mainArr );

    replaceIndexElement( mainArr, N, 2, 3 );

    for ( size_t i = 0; i < N; i++ ) 
    {
        printf( "%d\n", mainArr[i] );
    }

    return 0;
}

int replaceIndexElement ( int a[], size_t n, size_t index, int element )
{
    int success = index < n;

    if ( success )  a[index] = element;

    return success;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335