0

On page 42 of Effective C++, a pointer is used as an array name ala

AirPlane *newBlock = ...

newBlock[i].next=0;

I've not been aware that this is legal. Is this part of the c++ standard? Is it common practice?

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
Mike D
  • 2,753
  • 8
  • 44
  • 77
  • 1
    @Mike: there's no need pasting the full name of the book into the subject of all your questions – Eli Bendersky Jan 30 '10 at 14:48
  • 4
    If you are going to ask a lot of questions in this vein, you can expect a rapid reduction in your reputation here. Such basic questions are best answered by reading a good introductory book (Effective C++ is not introductory) on C++ - the fact that you are using a bad one is your problem, not ours. –  Jan 30 '10 at 14:49
  • @Neil: and yet someone is upvoting these questions so he actually gains reputation ... – Eli Bendersky Jan 30 '10 at 14:51
  • 2
    A good book list can be found here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Georg Fritzsche Jan 30 '10 at 14:54
  • @gf: Ok, I'll try another reference book and see what that does for me. – Mike D Jan 30 '10 at 15:17
  • @ Eli, Neil: Hopefully my trials will be of use to others. I'll try to space my questions out so they won't be a burden. – Mike D Jan 30 '10 at 15:20
  • @gf: What is Effective C++ doing in the beginner's section? :-D – Prasoon Saurav Jan 30 '10 at 16:21
  • @Prasoon: Hm, good question, should be in a section after that - fixed that. – Georg Fritzsche Jan 31 '10 at 00:28

3 Answers3

5

Yes, pointers can be used to dynamically allocate arrays of objects.

From this and other questions it appears that you're a newbie with C++. Therefore, starting with "Effective C++" isn't the best idea. While it's a great book, it's most useful for people already familiar with the language. You should start with an introductory book or tutorial, work through it, write some code and only then turn to "Effective C++".

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
2

From http://www.cplusplus.com/doc/tutorial/pointers/:

In the chapter about arrays we used brackets ([]) several times in order to specify the index of an element of the array to which we wanted to refer. Well, these bracket sign operators [] are also a dereference operator known as offset operator. They dereference the variable they follow just as * does, but they also add the number between brackets to the address being dereferenced. For example:

a[5] = 0;       // a [offset of 5] = 0
*(a+5) = 0;     // pointed by (a+5) = 0 

These two expressions are equivalent and valid both if a is a pointer or if a is an array.

danben
  • 80,905
  • 18
  • 123
  • 145
2

I would repeat answer I've given to similar question C strings confusion:

It is confusing indeed. The important thing to understand and distinguish is that char name[] declares array and char* name declares pointer. The two are different animals.

However, array in C can be implicitly converted to pointer to its first element. This gives you ability to perform pointer arithmetic and iterate through array elements (it does not matter elements of what type, char or not). As @which mentioned, you can use both, indexing operator or pointer arithmetic to access array elements. In fact, indexing operator is just a syntactic sugar (another representation of the same expression) for pointer arithmetic.

The very same rules are specified in C++ standard.

Also, take a look at Is array name a pointer in C?

Community
  • 1
  • 1
mloskot
  • 37,086
  • 11
  • 109
  • 136