int board[4][8] = {
{0, 1, 2, 3, 4, 5, 6, 7},
{1, 2, 3, 4, 5, 6, 7, 8},
{2, 3, 4, 5, 6, 7, 8, 9},
{3, 4, 5, 6, 7, 8, 9, 10},
};
int secondRow[8] = board[1]; // <-- error: invalid initializer
for (int i = 0; i < 8; i ) {
if (someCondition(secondRow[i])) {
// ...
}
}
How do I assign the second element in board
(the {1, 2, ..., 7, 8}
array) to secondRow
?
Do I declare secondRow
as a pointer of some sorts instead? If so, can I still use it in the same way in the loop below (or how should I change it)?
CodePudding user response:
You cannot. You must either copy it or reference it.
You probably want the latter:
int * secondRow = board[1];
for (int i = 0; i < 8; i ) {
if (someCondition(secondRow[i]))
...
}
Arrays are not first class citizens in C and C .
CodePudding user response:
Maybe not the answer you are looking for but if you are already looping through secondRow[i] why not assign values as you go?
for (int i = 0; i < 8; i ) {
secondRow[i]=board[1][i]; //Here
if (someCondition(secondRow[i])) {
// ...
}
}