My faq sheet for the #c and #cpp channels.

It seems that many times each week, somone asks these questions. I got sick of it, and decided to put together a FAQ sheet with reasonable answers and links to other resources.

"My program won't compile, why not?"

Nobody knows. Say something like this, instead:

"I'm running Windows XP, Visual C++ 6.0. I'm getting an error message 'Error C2036'. I pasted the code at the address in the channel topic. I expect the code fragment to connect to the Internet, but I can't figure out why it doesn't."

Be prepared to spend the next half hour or so working on the code.

"I'm having problems with the string class..."

Make sure you included <string> and not <string.h>

Make sure you have a statement 'using std::string;' after your include files and before your code.

"When do something in a function, my data isn't what I expect"

This usually is a scope issue. Common examples are when a function modifies a parameter, or the return value is a local array. A third option is to make the variable static. For example:


char * get_string()
{
   char buffer[128];
   /* do stuff */

   return buffer;
}

In this case, the array 'buffer' is de-allocated when get_string returns, so the contents of the return value are undefined. That means it might work sometimes and not work other times. It may crash, it may not. Another case:


void compute_square(int number)
{
  number *= number;
}

In this case, the parameter is unchanged. In c++ you can pass the parameter by reference, in c you need to pass a pointer to the parameter. If you don't understand pointers and references, read the next FAQ item.

"I have a 'char * buffer' that I can't use..."
"I have problems with the & operator..."
"How do I use pointers?"

Pointers are something at people new to the language struggle with. They also act as a good place to determine if a person is cut out to program computers, or not. This is mockingly referred to as the "I'm too stupid to use pointers" question, and you WILL hear that phrase if you ask the question. But ask it anyway.

If you prefer to avoid the ridicule, read any c or c++ programming book, and the read it again, focusing on 'pointers'. Try Ted Jensen's Pointer and Array tutorial. If you claim to be so improvished that you cannot purchase a book, try Bruce Eckel's books online, or read from the comp.lang.c FAQ, especially section 4 (pointers) and 5 (null pointers).

"I'm writing a game, and..."
"I need help writing a game..."
"I'm trying to write some 3D graphics..."
"I'm using DirectX and having problems with..."

This is the c or cpp channel. Try going to #gamedev for games, #OpenGL for 3D graphics or OpenGL specific stuff. If You don't know what COM is and you want to program DirectX, look at my introduction to COM

Valid XHTML 1.0! Best with any browser campaign