6

I never read anything about dereferencing arrays like pointers and I believe it shouldn't work. But the following code does work using QT Creator and g++ 4.8:

int ar[9]{1,2,3,4,5,6,7,8,9};
cout << *ar << endl; //prints the first element of ar

Is it proper behavior or just the compiler fixing the code?

2013Asker
  • 2,008
  • 3
  • 25
  • 36
  • 3
    It is OK. `ar` decays to a pointer to the first element of the array. So you are de-referencing that. – juanchopanza Jul 08 '13 at 18:05
  • Some people uses *(ar++) in a loop just like you can use an integer to track the position of the array the [] is just a pretty way to do that *(ar+index) works too – Lefsler Jul 08 '13 at 18:08
  • @Alexandru Barbarosie I started reading programming books a few weeks ago, so I'm not supposed to know it and searching on Google didn't return the answer. – 2013Asker Jul 08 '13 at 18:11
  • 1
    @demonofnight: In this case you can't use `*(ar++)`. This is only possible with a pointer, not with an actual array. – AnT stands with Russia Jul 08 '13 at 18:16
  • Not directly, but still a duplicate of [When is an array name or a function name 'converted' into a pointer ? (in C)](http://stackoverflow.com/questions/17506138/when-is-an-array-name-or-a-function-name-converted-into-a-pointer-in-c) – AnT stands with Russia Jul 08 '13 at 18:18

3 Answers3

5

You cannot dereference an array, only a pointer.

What's happening here is that an expression of array type, in most contexts, is implicitly converted to ("decays" to) a pointer to the first element of the array object. So ar "decays" to &ar[0]; dereferencing that gives you the value of ar[0], which is an int.

This recent answer of mine discusses this in some detail for C. The rules for C++ are similar, but C++ has a few more cases where the conversion does not occur (none of which happen in your code).

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Please, correct me if i'm wrong, but an array in c/c++ isn't always a pointer? – Lefsler Jul 08 '13 at 18:24
  • The array itself not, but the variable in c/c++ receive and address when you create an array, and that seems to happens everytime you create an array, i understand that an array is a sequence of elements in a sequencial memory area. but the variable receive the address of the first element, that was what i mean with always a pointer. The left side variable always receive the memory address of the first object in the array – Lefsler Jul 08 '13 at 18:32
  • @demonofnight, I'm not sure which variable you're referring to, but only when it decays. For example, `sizeof(ar)` will not cause any decay. – chris Jul 08 '13 at 18:35
  • @demonofnight: As I said, an *expression* of array type (including, but not limited to, the name of an array object) is implicitly *converted* to a pointer value in most contexts. Some of the cases where this doesn't occur are when it's the operand of unary `&` or `sizeof` operator. This doesn't happen when you *create* an array, only when an expression refers to it. And the array-to-pointer conversion of an array expression has no effect on any array object to which it may refer. – Keith Thompson Jul 08 '13 at 19:03
1

It is correct. The memory position is acquired and dereferenced so first 4 bytes is the integer. You can do *(ar+1) to get the second memory position, dereference that and get the first 4 bytes again. 4 bytes because it is an int if your machine is 32 bits and sizeof(int) == 4

Lefsler
  • 1,738
  • 6
  • 26
  • 46
  • It is because it needs to read 4 bytes (32 bits integer) from the position because it is an integer, if it was a char just one byte. and a 32bits memory pointer have, also, 4 bytes. if i said anything wrong please correct me, it has bees some time since i played with that. – Lefsler Jul 08 '13 at 18:09
  • 2
    What's incorrect is your assumption an `int` has to have 4 bytes. The size isn't guaranteed to be 4, it could just as well be 2 or 8 (usually it is 4, but that's not a rule) – Luchian Grigore Jul 08 '13 at 18:11
  • The correct formulation is that it reads `sizeof(int)` bytes. – Luchian Grigore Jul 08 '13 at 18:11
  • To be *really* pedantic about it, `sizeof (int) == 4` doesn't necessarily imply that it's 32 bits. `sizeof` is the size in bytes, and a byte has to be at least 8 bits, but it can be more. `CHAR_BIT`, defined in ``, tells you the number of bits in a byte. (You aren't likely to find `CHAR_BIT != 8` outside some exotic embedded systems, particularly DSPs.) – Keith Thompson Jul 08 '13 at 18:17
  • that is why i said 32 bits and sizeof = 32 you can put "an int as a char with the limit from 0 to 255" i understand you can have a int smaller than 32 bits :). – Lefsler Jul 08 '13 at 18:20
  • @demonofnight: That wasn't my point. You can have a conforming implementation with 16-bit `char` and 64-bit `int`. On such a system, `sizeof (int) == 4`, but an int is bigger than 32 bits. (In common usage, a "byte" is 8 bits; in C and C++, a "byte" is the size of a `char`, which is *at least* 8 bits but can be more.) – Keith Thompson Jul 08 '13 at 19:04
0

I believe that this is proper behavior. Pointers to an array always start at the head of the array--that is the first element. In this case that would be 'ar[0]', and the output should be '1'.

brthomps
  • 43
  • 6