The Place to Start


 Common Errors FAQ
BigGuy
Reference


Common Errors FAQ
Here are a few common errors that trouble newcomers.


Problem
Cause
Solution
Bad Entity in IndexOfEdict()  A model or sound not properly precached.  Check for models or sounds that you have not precached. Can be a simple misspelling of the name.
Can't get DLL API Dll in incorrect directory, wrong name, or not compiled correctly. Check your directory and name. Make sure your liblist.gam is configured correctly, learn how here.
Some other Common Programming Mistakes:
This list summarizes some of the more common programming mistakes, knowledge of these mistakes will help you prevent making them. They are not arranged in any particular order.

1. Misplacing a semicolon.

    if (j = 100);
        j=0;

In the above statements j will always be set to '0' because of the semicolon after the if statement.

2. Confusing the operator = with the operator ==.

    if (a=2)
        printf("Your turn. \n");

The above statements are perfectly valid and has the effect of setting a to 2 and then executing the printf call. The printf call will always be called because the if statement will always be nonzero (if its value 2).

3. Passing the wrong argument type to a function.

    result = square_root (2);

If the square_root function is expecting a floating point argument, then the above statement will produce erroneous results, since an integer value is being passed. Remeber the type cast operator can be used to explicitly force conversion of a value that is passed to a function.

4. Omitting return type declarations.

    result = square_root (value);

If square_root is defined later in the program, or in another file, and is not explicity declared otherwise, then the compiler will assume that this function returns a value of type int.

5. Confusing the precedences of the various operators.

    while (c=getchar() != EOF)
        ...
    if (x & 0xF == y)
        ...

In the first ecample, the value returned by getchar will be compared against the value of EOF first. The inequality has precedence over assignment therefore the c will contain a TRUE/FALSE result of the test.

In the second, the interget constant 0xF will be compared against y, since equality has higher precedence than any other bitwise operator. The result of this test (0 or 1) will then be ANDed with the value of x.

6. Confusing a character constant and a character string.
In this statement,

    text= 'a' ;

A single character is assigned to text.

    text="a";

A pointer to the character string "a" is assigned to text.
In the first text should be declared as char, in the second as a pointer to char.

7. Using the wrong bounds for an array.

    int a[100], i , sum=0;
        ...
    for ( i=1; 1<=100; i++)
        sum += a[i];
 

8. Forgetting to reserve an extra location in an array for the terminating null character of a string.

Remember to declare character arrays so that they are large enough to contain the terminating null character. For example, the string "hello" would require 6 locations in the character array.

9. Confusing the operator -> with the operator . when referencing structure members.

Remember the . operator is used for the structure variables, while the operator -> is used for the structure pointer variables.

10. Omitting the ampersand before nonpointer variables in a scanf call.

    int number;
        ...
    scanf("%d", number);

Remember that all arguments appearing after the format string in a scanf call must be pointers.

11. Using a pointer variable before its initialized.

    char *char_pointer;
    *char_pointer = 'X';

You can only apply the indirection operator to a pointer variable after you have set the variable to pointing somewhere.

12. Omitting the break statement at the end of a case in a switch statement.

Remember that if a break is not included at the end of a case, then execution will continue into the next case.

13. Inserting a semicolon at the end of a preprocessor definition

Theis usually happens because of the habit to end all lines with a semicolon.

    #define END_OF_DATA 999;

would lead to a syntax error if used in an expression such as

    if (value == END_OF_DATA)
        ...

14. Omitting parentheses around arguments in macro definitions.

    #define reciprocal (x) 1/x
        ...
    w = reciprocal (a+b);
 
The preceding assignment would be incorrectly evaluated as:

    w = 1 / a +b;

15. Leaving a blank space between the name of a macro and its argument list in the #define statement.

#define MIN (a,b)  ( ( (a) < (b) ) ? (a) : (b)  )

This definition is incorrect, as the preprocessor considers the first blank space after the defined name as the start of the definition for that name.

16. Using an expression that has side effects in a macro call.

    #define SQUARE(x)  (x) * (x)
        ...
    w = SQUARE(++v);

The invocation of the SQUARE macro will cause v to be incremented twice, since this statement will be expanded by the preprocessor to
 
    w = (++v) * (++v);
 

      Any questions or suggestions should be sent to me:bigguy@valveworld.com