-3

I am a Java guy trying to learn C++.

I came across some code where following the type name, there is a *. For example:

char* socialNum[125][9];

and

CSampleDoc* pDoc = GetDocument();

What does the star mean?

CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • if you see `*` in C/C++, it's generally either multiplication, or a pointer. in this case, pointer. – Marc B Oct 20 '13 at 21:32
  • 4
    I respectfully suggest that if you're trying to learn basic C++ syntax, do so with a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), rather than via Stack Overflow. – Oliver Charlesworth Oct 20 '13 at 21:32
  • @OliCharlesworth I was using a book but I was constantly skipping since so much is the same as in Java. It was hard to find the points that I didn't already know – CodyBugstein Oct 20 '13 at 21:34
  • 2
    @Imray: I suggest skipping back to the start. Other than superficialities, there are very few similarities between Java and C++. – Oliver Charlesworth Oct 20 '13 at 21:35
  • 2
    Don't take this the wrong way, but before you start asking "what does the `&` mean" and "what does the `&&` mean" and "what does the `virtual` mean", could you just grab a C++ text book and read it for a week? – Kerrek SB Oct 20 '13 at 21:37
  • Get a better book, or be a bit more diligent in reading the one you have. It is *critical* that you understand pointers, and you won't get a very complete understanding from SO posts. – Hot Licks Oct 20 '13 at 23:12
  • You should accept either @Martijn Courteaux 's answer or mine :) – Chris Cirefice Oct 21 '13 at 22:37

2 Answers2

2

That means that the variable is a pointer.

int myInt = 4;
int *myPointer = &myInt;

Now, myPointer points to the integer myInt. Pointing to something is basically holding the memory address of that something.


Since you said you come from Java, this in Java:

MyClass obj = new MyClass(); // obj is a reference (or pointer)

would be equivalent with this in C++:

MyClass *obj = new MyClass(); // obj is here a pointer as well.

// and once you are done with obj, don't forget to free the memory:
delete obj;
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

* means it is a pointer to a piece of data.

Take, for example, these:

int *myInt;
char *myChar;
MyClass *objectOfClass;

All of these are pointers to the data in memory. The value of the pointer is an int, which is the address (location) in memory of that data.

Chris Cirefice
  • 5,475
  • 7
  • 45
  • 75