Appending a second element
- Politechnika Śląska
- Fundamentals of Computer Programming
Appending a second element p = (struct stag*)malloc(sizeof(struct stag)); if (!p) exit(1); p-x = 13.0; p-next = head; /* link elements together */ head = p; ...
Ta witryna wykorzystuje pliki cookie, dowiedz się więcej.
Appending a second element p = (struct stag*)malloc(sizeof(struct stag)); if (!p) exit(1); p-x = 13.0; p-next = head; /* link elements together */ head = p; ...
Array names vs. Pointers • An array name is not a variable! = e.g. may not be incremented • Another way of accessing elements: t[i] *(t+i) • A curiosity: t[i] *(t+i) *(i+t) i[t] t[5] 5[t] /* yes, it works */ ...
Assigning names to types • typedef creates new data type names • typedef does not declare objects, but defines identifiers that name types ( typedef names ) • typedef does not introduce new types • … only synonyms for types that could be specified...
Avoiding goto • Code involving a goto can always be written without one, which may require more coding (additional variables and tests): found = 0; for (i = 0; i ...
Calling a function /* test the power function */ int main() { int i; for (i = 0; i (…) … Calling a function /* test the power function */ int main() { int i; for (i = 0; i < 10; i++) printf("%d %d %d\n", i, power(2,i), power(-3,i));...
Character Output • Writing a single character or EOF: int putc(int c, FILE *stream); • Example: putc('A',stdout); • Writing a single character to stdout : int putchar(int c); • putchar('A') wor...
Clearerr Resets the error indicator for a stream void clearerr( FILE * stream ); • resets the error indicator and end-of-file indicator for stream • Error indicators are not automatically cleared • Once the error indicator for a specified stream is set, operations on that stream continue to return ...
Closing a file • The function int fclose(FILE *fp) - inverse of fopen • Most operating systems have some limit on the number of simultaneously opened - free the file pointers when they are no longer needed • fclose on an output file flushes the buffer • fclose is called automatically for each open ...
Command line arguments • A program may be called from a command line • It may accept arguments (parameters) • When main is called, it is called with (at least) two arguments: - the number of arguments, including the program name itself; usually ca...
Conditions in C • There is no special type for logical values - other languages have boolean, bool etc. • Instead, integers are used: - 0 means false - any other value means true • Condition expressions return 0 or 1 • Condition expressions may...