int array1[4], array2[4] = {3, 6, 9, 12};
array1 = array2;

and this one:

void showValues (int nums [4][])
{
  for (rows = 0; rows < 4; rows++)
    for (cols = 0; cols < 5; cols++) // is this the line of error? cols is not declared for a size.
        cout << nums[rows][cols];
}

Dani AI

Generated

Two separate problems are visible in ’s examples: built‑in C arrays cannot be assigned with = (they decay to pointers in many expressions but are not assignable), and the 2‑D parameter/loop code uses an invalid parameter form and undeclared index variables. is correct that an element‑by‑element copy is required for C arrays; is correct that arrays behave like a pointer-to-first-element but cannot be "reseated."

Element‑by‑element copy (C style) or using the STL:

int arr1[4];
int arr2[4] = {3, 6, 9, 12};
for (size_t i = 0; i < 4; ++i)
    arr1[i] = arr2[i];

or

#include <algorithm>
std::copy(arr2, arr2 + 4, arr1);

Modern C++ makes this simpler. For fixed-size collections prefer std::array, which supports direct assignment:

#include <array>
std::array<int,4> a1, a2 = {3, 6, 9, 12};
a1 = a2; // allowed and safe

For the 2‑D case prefer a container that carries its dimensions (no compile-time column requirement) and declare loop indices locally. Example using std::vector:

#include <vector>
#include <iostream>

void showValues(const std::vector<std::vector<int>>& nums) {
    for (size_t r = 0; r < nums.size(); ++r) {
        for (size_t c = 0; c < nums[r].size(); ++c)
            std::cout << nums[r][c] << ' ';
        std::cout << '\n';
    }
}

Notes and cautions: avoid globals for rows/cols; prefer size_t for indices; use const where possible; memcpy can copy POD arrays but is error‑prone for non‑POD types (prefer std::copy or container assignment).

Recommended Answers

All 2 Replies

You cannot assign (the contents of) one array to another with a simple assignment statement. You have to write code that assigns from array2 to array1 element by element.

In your function, the parameter must give the column dimension (second set of [ ] ), the row dimension doesn't matter. Since the number of rows can vary, it's typical to pass the row dimension as an additional parameter. Thus, you can write the function so that it can deal with array of any number of rows. Like this:

void showValues (int nums [ ][5], int n_rows )
{
   int r, c;
   for( r = 0; r < n_rows; r++ )
      for( c = 0; c < 5; c++ )
         cout << nums[r][c] << "  ";
}
int array1[4], array2[4] = {3, 6, 9, 12};
array1 = array2;

and this one:

void showValues (int nums [4][])
{
  for (rows = 0; rows < 4; rows++)
    for (cols = 0; cols < 5; cols++) // is this the line of error? cols is not declared for a size.
        cout << nums[rows][cols];
}

In your first chunk of code, you could omit the 4 from the declaration of array2.

But the real issue is that array2 is a reference variable that cannot hold a variable amount of addresses. It cannot play the role of a pointer.

Although C++ treats arrays as pointers, they are more or less pointer * const types.
For example...

int val[4]; // array declared on the stack - cannot point to another address
int* const val2 = val; // declares a pointer thats contents can be modified but can only point to one address

in your second chunk of code, neither rows nor cols are declared inside your method, but they are assigned and used. I guess this is fine if they are global variables, but it isn't a best practice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.