1

This code has to copy one array to another via void copy function. But I don't understand why it doesn't work.

#include <stdio.h>

void copy(int func_array_1[], int func_array_2[], int size) {
    for (int i = 0; i < size; i++)
        func_array_1[i] = func_array_2[i];
}

int main() {

    int size = 0;
    int array[10] = { 0 };
    int copy_array[10] = { 0 };
    printf("Input the number of elements to be stored in the array :");
    scanf("%d", &size);

    for (int i = 0; i < size; i++)
        scanf("%d", &array[i]);

    copy(array, copy_array, size);

    for (int i = 0; i < size; i++)
        printf("%d", copy_array[i]);

    return 0;
}

It gives first defined array members which are all zero.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
alper ortac
  • 49
  • 1
  • 6
  • You have a typo func_array_1[i] = func_array_2[i]; You need to write func_array_2[i] = func_array_1[i]; – Vlad from Moscow May 13 '22 at 20:11
  • What is the input you give the program? What is the output you get from the program? What is the output you *expect* from the program? Please take some time to refresh [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/). And don't forget how to [edit] your questions. – Some programmer dude May 13 '22 at 20:11
  • I also suggest this is a good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. Also please read [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Some programmer dude May 13 '22 at 20:12
  • Your `copy` function copies from the second array to the first array. But you're calling it with the two arrays reversed. Decide what argument order you want, then use it consistently. Also, I think you gave up too soon in your debugging attempts. You should have been able to find this on your own. – Tom Karzes May 13 '22 at 20:13
  • Also, since you're programming in C you should probably use [*variable-length arrays*](https://en.wikipedia.org/wiki/Variable-length_array) rather than hardcode the size using the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)) `10`. It will make it *very* easy to enter a too large value for `size`. – Some programmer dude May 13 '22 at 20:15

2 Answers2

3
for (int i = 0; i < size; i++)
    func_array_1[i] = func_array_2[i];      

You're copying from the zero-initialized array to the one you actually want to copy to. So just change the loop body to:

func_array_2[i] = func_array_1[i];

Using const and / or more purposeful naming could've prevented your error.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
D-I-N-O
  • 46
  • 3
2

You have a typo in the function

for (int i = 0; i < size; i++)
    func_array_1[i] = func_array_2[i];      
    ^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^

As the function is called like

copy(array, copy_array, size);

you have to write

for (int i = 0; i < size; i++)
    func_array_2[i] = func_array_1[i];      
    ^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^

The reason of the typo is a bad function declaration. The function should be declared at least like

void copy( const int func_array_1[], int func_array_2[], int size);

That is the array that is not being changed within the function should be declared with the qualifier const.

Instead of the for loop you could use the standard function memcpy declared in the header <string.h> as for example

memcpy( func_array_2, func_array_1, size * sizeof( int ) );

Pay attention to that you need to check that the entered value of the variable size is not greater than 10.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335