-4

My teacher mentioned (actually he just wrote it on the board and said nothing about it) this particular pointer but I don't understand the significance of it. The example he wrote on the board was this:

Book * book;

What does the pointer do when it is used like this?

grim_v3.0
  • 387
  • 1
  • 4
  • 11
  • 3
    Wouldn't the solution to this be `ASK THE TEACHER???` – John3136 Jan 22 '14 at 06:28
  • @AndreyChernukha ... but I can't find a fitting close reason. – Mark Garcia Jan 22 '14 at 06:30
  • My teacher is for some reason has issues understanding his students' questions. – grim_v3.0 Jan 22 '14 at 06:31
  • @JerryCoffin It's not a duplicate, asker doesn't know the significance of * operator, he is not asking about it's usage with `const` – Pratik Singhal Jan 22 '14 at 06:33
  • @ps06756: Though that question *also* includes `const`, it (and the answers to it) also talk about the (in)significance of white space. – Jerry Coffin Jan 22 '14 at 06:35
  • @Spook: See his comment to Paul Draper's answer, which makes it clear that whitespace is *exactly* what he doesn't (or didn't) understand. – Jerry Coffin Jan 22 '14 at 06:40
  • Ok, I was judging basing on his question, not comments to answers. I guess, that a duplicate will be a good close reason after all. – Spook Jan 22 '14 at 06:41
  • Okay, I just have to ask, why does the duplicate link point to a question that is marked as yet *another* [duplicate](http://stackoverflow.com/questions/2660633)? And then why is that marked as a duplicate of [two](http://stackoverflow.com/questions/558474) [more](http://stackoverflow.com/questions/377164)? The first of which, by the way, is marked a duplicate of [another](http://stackoverflow.com/questions/398395)! Why not point to the original, rather than an entire chain of duplicate questions? – Paul Draper Jan 22 '14 at 06:50

2 Answers2

1

The * is part of the type.

book is of type Book *, that is a pointer to a Book.

class Book {
};

Book * book1 = new Book();
Book * book2 = book1;
//now book1 and book2 point to the same Book
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
0

Basically, using * can be used to define a pointer.
What your teacher meant was to declare a pointer to a object of type Book

    Book* book1  ; // Declare a pointer to a object of type Book
    Book bookObj ; 
    book1 = &bookObj ; // now book1 points to bookObj.  

You can use * with any type(predefined as well as custome defined types in c++).
You can also use * to dereference a pointer.

   int num1 = 0 ; 
   int* num1p = &num1 ;  // pointer pointing to num1
   *num1p = 10 ;  // same as num1 = 10.

Hope this helps!

Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97