0

Why can't I do something like this in C++? (Unlike in JavaScript or PHP)

int matrix[100][100], ar[100];

//...reading the matrix...

ar = matrix[0];

I would expect ar to contain matrix[0][0], matrix[0][1]...matrix[0][100]. Instead, it just returns me a memory address. Why doesn't this work (what's the exact feature that C++ does not have, are there any disadvantages/advantages? which?) and how can I replicate this behaviour or at least the same task. (without having to loop and copy)

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • It's not a duplicate. I know how to use arrays in C++, I just don't know how to use them in this certain situation. –  Apr 15 '14 at 21:21
  • Actually, I do. I don't know how to use them properly in this specific language. Therefore, what you have just said is nonsense. Moreover, I did not ask "how to use arrays in C++", so this cannot be a duplicate of that question. Another nonsense. So, everything you have ever said in this context is invalid. Now please leave this topic before shooting around with more nonsense. –  Apr 15 '14 at 21:26
  • 1
    You would know the answer to this question if you knew how to use arrays in C++, so the two questions are essentially identical. The problem is that you don't know how to use arrays in C++. – Puppy Apr 15 '14 at 21:34
  • @DeadMG Have you ever used a telephone? Or a car? Or a computer? Yes you did. You also did know how to use them. But at some point, a problem of understanding a core concept of one of this things interfered and you had to ask for help. You don't have to know how to build a batmobile in order to know how to use the pedals. End of story. –  Apr 15 '14 at 21:37
  • @AndreiPham, I'm sorry for the misinterpretation. What I actually meant is that you probably know how to use array in other languages, but you don't appear to know them in C++ (and they are a whole lot different from most languages). What I've, therefore, recommended is for you to look at the linked question I've provided in which it carefully explains what arrays are in C++. – Shoe Apr 15 '14 at 21:37
  • @AndreiPham, and I also know that the answer to this question is there (in the linked question), because, unlike you, I've read it. – Shoe Apr 15 '14 at 21:38
  • @Jefffrey I realised that my answer was there only after you told me. How on earth was I supposed to know (just by browsing through a question list before posting) that my specific problem is solved in that question. –  Apr 15 '14 at 21:42
  • @AndreiPham, by admitting that you don't have much of a clue about what arrays are **in C++** and searching "arrays in C++ how do they work" or any similar query. Otherwise you don't show much of a research effort. – Shoe Apr 15 '14 at 21:47
  • @Jefffrey I thought my problem was a weird one, I did not know it is so basic. –  Apr 15 '14 at 21:49
  • @AndreiPham, no problem. In the future just remember to do some researching before asking. It saves a lot of time on both ends. Good day. :) – Shoe Apr 15 '14 at 21:57

5 Answers5

1

C++ assignments (operator =) works by value, others languages, like php and javascript assign by reference.

In order to do what you want, you need to work with pointers:

int matrix[100][100]; // As a pointer, this can be seen as int **
int *row;

row = matrix[0];

int value = row[50];
Bruno Ferreira
  • 1,621
  • 12
  • 19
  • But...? What else should you add? – Shoe Apr 15 '14 at 21:23
  • @Jeffrey Sorry, I don't get what you mean. I tried to keep my answer as clean as possible so it is easy to understand. I know that it, under the roof, is not like that, but for a starter, that should do. – Bruno Ferreira Apr 15 '14 at 21:28
  • This is not an optimal solution in C++. Do you know why? – Shoe Apr 15 '14 at 21:34
  • Why is that? Does it occupy more memory or what? –  Apr 15 '14 at 21:47
  • @Jefffrey I can't point it out – Bruno Ferreira Apr 15 '14 at 21:49
  • Because `row` is now an "alias" of `matrix`, every modification to `matrix` will affect `row` and viceversa. Not to mention that if you pass `row` anywhere you have effectively lost the dimension of the array you are pointing to. – Shoe Apr 15 '14 at 21:55
  • And if you actually want an alias, you should use a reference instead. – Shoe Apr 15 '14 at 21:56
0

When you remove one extent from an array, you are left with an indirection or pointer.

So:

int matrix[100][100], *ar;
// ... doing stuff ...
ar = matrix[0];

Will work.

matrix[x][y] -> is an int

matrix[x] -> is a pointer to an int (or you could say an array of ints starting at that address)

You could also initialize ar to begin at any place in the matrix:

ar = &matrix[39][22]; //eg.
qeadz
  • 1,476
  • 1
  • 9
  • 17
  • Alright, I get it, I have to work with pointers. I just didn't realise that matrix[0] is a pointer itself, I thought it's the specific int array. –  Apr 15 '14 at 21:43
0

ar have type int[100]. In any expression (except sizeof ar and &ar) it will decay to int *. And matrix will decay to int (*)[100]. So, you can't just assign these pointers.

You can't modify them, but you can take their addresses (it will be equal to address of array).

And another thing: in fact, these pointer are not always exist in the memory. And address of decayed array will be equal to address of array itself and address of it's first element.

int arr[10]{};
std::cout << (int)arr << "\t" << &arr << "\n"; // 0x0001bfcd 0x0001bfcd 
std::cout << (int)arr[0] << "\t" << &arr[0] << "\n"; // 0 0x0001bfcd 
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
0

Why can't I do something like this in C++?

Because you are not actually using any C++ feature at all. You should use std::array which will provide copy semantic for you:

std::array<std::array<int, 100>, 100> matrix;
auto ar = matrix[0];

Live example

you should be aware, though, that this will make a copy. If you want to just access a particular array you can use a const/non-const reference instead:

std::array<std::array<int, 100>, 100> matrix;
const auto& ar = matrix[0];

Live example

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I didn't downvote, but have a question: Is this guaranteed to have the same memory-layout as matrix[100][100]? Do you have a ref or link? – André Apr 16 '14 at 07:25
  • @Andre, [cppreference says](http://en.cppreference.com/w/cpp/container/array) "This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member." – Shoe Apr 16 '14 at 12:51
0

Instead, it just returns me a memory address.

That is correct: matrix[0] is a pointer to an array of 100 ints, while ar is the address of an array of 100 ints. You can assign an address to a declared pointer, but you cannot assign a pointer, declared or implicit, to a declared address. The actual code

ar = matrix[0]

should get you a compilation error on the lines "incompatible types in assignment".

Why doesn't this work

What do you mean by work? What do you want to do?

If you want to address a particular "row" of the matrix, such as row# 0, you can do this

int *ar;
ar = matrix[0];

and then use subscript syntax to access a particular cell in the array

ar[50] = 42; // same as matrix[0][50] = 42

(without having to loop and copy)

Copies of arrays need loops.

arayq2
  • 2,502
  • 17
  • 21