1

I know enough that a * relates to a pointer. I'm still trying to sort that out in my head (pointers versus references.)

I'm working through a C++ book and there is a method signature in it like this:

void DrawBitmap(char *filename, int x, int y)

What does the * mean in this situation? is it accepting a pointer, or a reference to a variable?

Thanks for any help... and for putting up with an admittedly noob question.

quakkels
  • 11,676
  • 24
  • 92
  • 149
  • It expects an _array_ of `char`-s (the C way of representing a string). The array is passed as a pointer to the first element. – James M Apr 14 '12 at 20:10
  • 7
    @JamesMcLaughlin In fact it is a pointer rather than an array – David Heffernan Apr 14 '12 at 20:11
  • You need to go back to the book really – David Heffernan Apr 14 '12 at 20:11
  • 3
    If you have a **C++** book teaching you to do things like that, then you might be learning from a very outdated book. It looks as if it's trying to teach you the C language first (That used to be the done thing back in the 1990s, but not in 2012). C++ includes a `string` type for handling strings - passing _char*_ to a function is a very outdated way of doing things. – Ben Cottrell Apr 14 '12 at 20:14
  • 1
    the book is Beginner Game Programming with C++. I'm actually a bit disappointed with it because it doesn't really explain C++ that much. At one point, the author even says that instead of explaing pointers he rather that readers of his book just use them and we'll get it eventually. – quakkels Apr 14 '12 at 20:15
  • 4
    Time to get a better book. Don't worry about game programming. Learn to program, and then think about game specific techniques. For books, refer to this excellent Q: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – David Heffernan Apr 14 '12 at 20:16
  • I've been professionally programming C# for a couple years now. My programming knowledge so far has been higher level to this point. I want to get in games programming as a hobby and I would LOVE to learn a foundational language like C++. Thanks for the link too. – quakkels Apr 14 '12 at 20:20

3 Answers3

5

It means that you're passing it a pointer to a character, which usually means that pointer points to the first character in an array of characters. With a pointer (*), you can do arithmetic, e.g. (fileName + 1) to get the second character. When you use a reference (&), you are implying that the receiving function should operate on the original data. Without the reference, the function is passed a copy, rather than the original.

Gordon
  • 1,844
  • 4
  • 17
  • 32
1

Others have directly answered your question already; some of the following links are well worth a read for information about pointers (and their syntactical relationship to arrays in C++), they're well worth studying and I would recommend taking a little time reading through them to get your head around the ideas:

http://www.c-faq.com/aryptr/

http://www.augustcouncil.com/~tgibson/tutorial/ptr.html

http://www.augustcouncil.com/~tgibson/tutorial/arr.html

http://www.eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx

http://www.daweidesigns.com/cgi-bin/pointers.php

Ben Cottrell
  • 5,741
  • 1
  • 27
  • 34
0

char *filename is pointer to a character you are passing to DrawBitmap method. For more details please see this link.

ABH
  • 3,391
  • 23
  • 26