2

In c++ identifier of an array is a pointer and in java identifier of an array is a reference variable(practically a pointer).

Let's say there are an array a and b. How come in java this operation is allowed:

a = b; //the reference that 'b' holds will be copied to 'a' so both a and b point to the same array

but in C++ the same action will be considered an invalid assignment.

If a and b are both pointers in c++, why address that b holds won't be copied to a?

9
  • 1
    Your first statement is incorrect. This affects what follows. A C++ array is not a pointer. Still, the language could have supported array assignment, so the general question is good. Commented Jan 2, 2014 at 8:38
  • They are different langauges, so they behave differently. Commented Jan 2, 2014 at 8:38
  • 1
    OK, I meant "In c++ identifier of an array is a pointer". This is incorrect. What happens is that in many contexts, they decay to pointers. This comes from the C language. Commented Jan 2, 2014 at 8:40
  • 1
    @Devolus i know that but it should make sense.if the identifiers are pointers they should act like one Commented Jan 2, 2014 at 8:41
  • 1
    In C++ an array is not a pointer. It decays to a pointer if you pass it around (like passing it to a function). Commented Jan 2, 2014 at 8:44

2 Answers 2

4

Arrays are not really pointers, they just degrade down to pointers (which point to the first element they contain.) They actually are blobs of data. You can't change the location of an array in C++ because there is no place in memory where a pointer is stored; it is merely an offset into a structure or the stack frame.

Sign up to request clarification or add additional context in comments.

3 Comments

But you could have an assignment operator that copies the contants of one array into another, instead of having to call, say, std::copy.
But that'd be unintuitive in my opinion, since arrays degrade to pointers (they're not orthogonal to other C designs, like structures.) If you want C++-like behavior, I'd say you should try std::array - the only downsides being that multidimensional syntax sucks. (Apparently, there is a C++11 solution to that problem.)
I agree. It is because arrays behave in a peculiar way in C that it would be confusing to "extend" their interface with assignment etc. That is probably one of the reasons behind this.
0

In C++ the array references are const pointers but in Java they are neither pointers nor implicitly constant.

2 Comments

@T.J.Crowder Yeah i hope now the answer looks better.
Well, "...are implicitly const pointers..." is incorrect. Arrays are their own thing in C++. As juanchopanza has pointed out, in many cases they degrade to pointers to the first element of the array, but that's not the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.