20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Common <strong>Programm<strong>in</strong>g</strong> Mistakes<br />

499<br />

7. Forgett<strong>in</strong>g to reserve an extra location <strong>in</strong> an array for the term<strong>in</strong>at<strong>in</strong>g null character of a<br />

str<strong>in</strong>g.<br />

Remember to declare character arrays so that they are large enough to conta<strong>in</strong> the<br />

term<strong>in</strong>at<strong>in</strong>g null character. For example, the character str<strong>in</strong>g "hello" would<br />

require six locations <strong>in</strong> a character array if you wanted to store a null at the end.<br />

8. Confus<strong>in</strong>g the operator -> with the operator . when referenc<strong>in</strong>g structure members.<br />

Remember, the operator . is used for structure variables, whereas the operator -><br />

is used for structure po<strong>in</strong>ter variables. So, if x is a structure variable, the notation<br />

x.m is used to reference the member m of x. On the other hand, if x is a po<strong>in</strong>ter to<br />

a structure, the notation x->m is used to reference the member m of the structure<br />

po<strong>in</strong>ted to by x.<br />

9. Omitt<strong>in</strong>g the ampersand before nonpo<strong>in</strong>ter variables <strong>in</strong> a scanf call.<br />

Example<br />

<strong>in</strong>t number;<br />

...<br />

scanf ("%i", number);<br />

Remember that all arguments appear<strong>in</strong>g after the format str<strong>in</strong>g <strong>in</strong> a scanf call<br />

must be po<strong>in</strong>ters.<br />

10. Us<strong>in</strong>g a po<strong>in</strong>ter variable before it’s <strong>in</strong>itialized.<br />

Example<br />

char *char_po<strong>in</strong>ter;<br />

*char_po<strong>in</strong>ter = 'X';<br />

You can only apply the <strong>in</strong>direction operator to a po<strong>in</strong>ter variable after you have set<br />

the variable po<strong>in</strong>t<strong>in</strong>g somewhere. In this example, char_po<strong>in</strong>ter is never set<br />

po<strong>in</strong>t<strong>in</strong>g to anyth<strong>in</strong>g, so the assignment is not mean<strong>in</strong>gful.<br />

11. Omitt<strong>in</strong>g the break statement at the end of a case <strong>in</strong> a switch statement.<br />

Remember that if a break is not <strong>in</strong>cluded at the end of a case, then execution<br />

cont<strong>in</strong>ues <strong>in</strong>to the next case.<br />

12. Insert<strong>in</strong>g a semicolon at the end of a preprocessor def<strong>in</strong>ition.<br />

This usually happens because it becomes a matter of habit to end all statements<br />

with semicolons. Remember that everyth<strong>in</strong>g appear<strong>in</strong>g to the right of the def<strong>in</strong>ed<br />

name <strong>in</strong> the #def<strong>in</strong>e statement gets directly substituted <strong>in</strong>to the program. So the<br />

def<strong>in</strong>ition<br />

#def<strong>in</strong>e END_OF_DATA 999;<br />

leads to a syntax error if used <strong>in</strong> an expression such as<br />

if ( value == END_OF_DATA )<br />

...

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!