|
|||
Department of Engineering | |
University of Cambridge > Engineering Department > computing help |
if (i=3) return 1; else return 0;will always return 1 because the assignment `i=3' has the value 3 and 3 is true! gcc's warning option can alert you to this. You might also try to get into the habit of writing expressions like if (3==i) to safeguard yourself from this kind of error.
if (i=6) z=mean(x,y); /* get the xy mean */ mean(z,y);If you decided not to risk running mean you might do
/* comment this fragment out if (i=6) z=mean(x,y); /* get the xy mean */ mean(z,y); */but it wouldn't work because the first `/*' would be matched by the `*/' on the `mean(x,y)' line (the `/*' on that line being ignored), and `mean(z,y);' wouldn't be commented out at all. In this case the final `*/' would be flagged as an error, but you won't always be so lucky.
... i = 3; j = 10; while (i<100); i = i+j; ...This while loop will go on for ever. The semicolon after the while condition is a null statement which forms the body of the loop so i will always be 3. Take away that semicolon and
i = i+j
becomes the body, which is
probably what was intended.
if (foo) if (bar) win (); else lose ();(the else matches the closest if), always like this:
if (foo) { if (bar) win (); else lose (); }
... if (i==7) j = 7; k = 7; ...
a[i++] = b[i++];will have different results according to whether or not the i on the left-hand side is calculated before the right-hand side is evaluated.
... FILE *fp; ... if (fp=fopen(filename, "r") == NULL) return (NULL);Here the intention is to try opening a file, then compare the resulting fp to NULL to see if fopen failed. Unfortunately, what actually happens first is the test (fopen(filename, "r") == NULL) which has an integer result (non-zero if the statement is true). This result is then assigned to fp. The compiler should warn you about this problem. The code should have some extra brackets:-
... FILE *fp; ... if ((fp=fopen(filename, "r")) == NULL) return (NULL);
~
' character
needs to be interpreted by the shell.
if ((fp=fopen("~/data", "r")) == NULL) return (NULL);You'll have to find out your home directory (use getenv("HOME")) and append to it.
... int i; scanf("%d",i); /* this should be scanf("%d",&i) */
#define BUFLEN 10 int x[BUFLEN], y; ... if (y >= BUFLEN || y<0) [error code here] else x[y] = 255; ...
'\0'
and "\0"
. Suppose
you had
char str[100]; char *str_ptr; str_ptr = str;then str_ptr and str would both point to the first element in the array. Suppose you wanted to initialise this string by making the first element a zero byte. You could do
strcpy(str_ptr, "\0") /* or strcpy(str_ptr, "") */or
*str_ptr = '\0';but
str_ptr = "\0";would do something quite different. It would create a string in your executable (namely
"\0"
) and set str_ptr to point to it
with potentially disastrous effects.