Commit d6a9abef authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Some fixes/extensions of chapter 7 comments

parent 946e8688
......@@ -14,7 +14,6 @@
#include <stdio.h>
int average (int val1, int val2) { return val1 + val2 / 2; } // function returns int, but result of calculation may be with decimal places
// additionally it is advisable to always provide function prototypes
int main (int argc, char** argv) { // argc & argv are not used within this program
char number1 = 15;
......
......@@ -20,6 +20,6 @@ int main (int argc, char** argv) {
scanf("%c",&answer);
if (answer == 'n') {
printf("What do you want then? ");
scanf("%s",maoam);
scanf("%s",maoam); // scanf with "%s" implies to danger of buffer overflows
}
} // main function has return type int, but return statement is missing
......@@ -15,7 +15,7 @@
int main (int argc, char** argv) { // argc & argv are not used within this program
char animal[] = 'frog'; // strings are denoted with "", '' are only applicable for single character
int count = sizeof(animal);
int count = sizeof(animal); // it is advisable to always take into account the size of the variable type in sizeof()
for (int i=0; i <= count; i++); // semicolon after loop head ends the loop ...
printf("%d letter: %c\n",i,*animal+i); // ... therefore this statement is executed only once
// loop head run condition has an off-by-one error, since the maximum index of animal is (count-1) ...
......
......@@ -13,7 +13,7 @@
// required includes are missing (i.e., <stdio.h>)
int* get_double (int value) { // it is advisable to always provide function prototypes
int* get_double (int value) {
value *= 2;
return &value; // the memory location of value is automatically freed after leaving the function
// therefore its address is invalid afterwards
......@@ -23,7 +23,7 @@ int main (int argc, char** argv) {
int number = 42;
int* number2x = get_double(number);
printf("Double a value using an own function!\n");
printf("The double of %d is %d!\n",number,*number2x);
printf("The double amount of %d is %d!\n",number,*number2x);
return 0;
}
......@@ -15,7 +15,7 @@
int check = 0;
void check_input (int value, int guess) { // it is advisable to always provide function prototypes
void check_input (int value, int guess) {
int check = value == guess ? 1 : 0; // declaration of local variable check shadows global variable
return;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment