C Program to Copy One Array to Another

C Program to Copy One Array to Another

A class stores five marks in their original order: 40, 35, 45, 35 and 50. The copy an array task needs a clear rule about order, duplicates and how many elements exist.

Copy each element into a separately sized destination array when the destination needs independent integer storage.

Before this exercise, review array elements and bounds and function parameters. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why copy an array uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Change the copied second mark to 38 and verify that the original second mark remains 35.

Copy an Array: follow the data

  1. Given data

    A class stores five marks in their original order: 40, 35, 45, 35 and 50. The copy an array task needs a clear rule about order, duplicates and how many elements exist.

  2. Rule

    Copy each element into a separately sized destination array when the destination needs independent integer storage.

  3. Observed result

    Original: 40; copy: 99

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

An array is not assigned wholesale with copy = marks in C. This loop performs five individual assignments, then demonstrates independence by changing only the destination.

The complete example knows its five-element bound. In a reusable function, pass an explicit element count with the array pointer; a pointer alone does not report that bound. This sample has two equal marks, which makes duplicate handling observable.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int copy[5];
  for (int i = 0; i < count; i++) copy[i] = marks[i];
  copy[0] = 99;
  printf("Original: %d; copy: %d\n", marks[0], copy[0]);
  return 0;
}

Expected output

Original: 40; copy: 99
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int copy[5];
  for (int i = 0; i < count; i++) copy[i] = marks[i];
  copy[0] = 99;
  printf("Original: %d; copy: %d\n", marks[0], copy[0]);
  return 0;
}

copy receives the five current marks; replacing copy[0] does not change marks[0].

Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.

Follow the worked calculation

  1. Start with the sample

    copy receives the five current marks; replacing copy[0] does not change marks[0].

  2. Apply the rule

    Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.

  3. Check the result

    Original: 40; copy: 99

    Original: 40; copy: 99
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int copy[5];
  for (int i = 0; i < count; i++) copy[i] = marks[i];
  copy[0] = 99;
  printf("Original: %d; copy: %d\n", marks[0], copy[0]);
  return 0;
}

Change the copied second mark to 38 and verify that the original second mark remains 35.

Worked answer

The complete answer keeps the five valid input elements and applies the stated alternative. Read the output against the original array rather than assume that every loop should sort, remove or overwrite values.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int copy[5];
  for (int i = 0; i < count; i++) copy[i] = marks[i];
  copy[1] = 38;
  printf("Original: %d; copy: %d\n", marks[1], copy[1]);
  return 0;
}

Expected output

Original: 35; copy: 38
Run in C compiler →

Recognize the important boundaries

The destination capacity must cover every copied element. memcpy can copy representation bytes when its size and nonoverlap contracts are correct.

The sample marks and their sums fit int. General input requires both valid array bounds and an arithmetic-range contract.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why is count passed separately to a reusable array function?
Choose one answer. You can try again.

Answer and explanation

The element pointer does not carry the caller’s element count.

Correct. The separate bound defines which accesses are permitted.

Use the idea independently

Copy each element into a separately sized destination array when the destination needs independent integer storage.

Continue with array indexes and bounds.

Continue with pointer and count contracts.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program for Array Rotation →