Update
This commit is contained in:
parent
b5ef148d94
commit
8db8b687bf
3 changed files with 15 additions and 14 deletions
|
@ -1230,9 +1230,9 @@ But what can pointers be good for? Many things, for example we can kind of "stor
|
|||
```
|
||||
#include <stdio.h>
|
||||
|
||||
int backAccountMonica = 1000;
|
||||
int backAccountBob = -550;
|
||||
int backAccountJose = 700;
|
||||
int bankAccountMonica = 1000;
|
||||
int bankAccountBob = -550;
|
||||
int bankAccountJose = 700;
|
||||
|
||||
int *payingAccount; // pointer to who's currently paying
|
||||
|
||||
|
@ -1255,7 +1255,7 @@ int main(void)
|
|||
{
|
||||
// let Jose pay first
|
||||
|
||||
payingAccount = &backAccountJose;
|
||||
payingAccount = &bankAccountJose;
|
||||
|
||||
payBills();
|
||||
buyFood();
|
||||
|
@ -1263,7 +1263,7 @@ int main(void)
|
|||
|
||||
// that's enough, now let Monica pay
|
||||
|
||||
payingAccount = &backAccountMonica;
|
||||
payingAccount = &bankAccountMonica;
|
||||
|
||||
buyFood();
|
||||
buyGas();
|
||||
|
@ -1272,16 +1272,16 @@ int main(void)
|
|||
|
||||
// now it's Bob's turn
|
||||
|
||||
payingAccount = &backAccountBob;
|
||||
payingAccount = &bankAccountBob;
|
||||
|
||||
payBills();
|
||||
buyFood();
|
||||
buyFood();
|
||||
buyGas();
|
||||
|
||||
printf("Monika has $%d left.\n",backAccountMonica);
|
||||
printf("Jose has $%d left.\n",backAccountJose);
|
||||
printf("backAccountBob has $%d left.\n",backAccountBob);
|
||||
printf("Monika has $%d left.\n",bankAccountMonica);
|
||||
printf("Jose has $%d left.\n",bankAccountJose);
|
||||
printf("Bob has $%d left.\n",bankAccountBob);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1317,7 +1317,7 @@ int main(void)
|
|||
}
|
||||
```
|
||||
|
||||
Function `getUnitCirclePoint` doesn't return any value in the strict sense, but thank to pointers it effectively returns two `float` values via its parameters `x` and `y`. These parameters are of the data type pointer to `int` (as there's `*` in front of them). When we call the function with `getUnitCirclePoint(i * 0.125 * 2 * PI,&pointX,&pointY);`, we hand over the addresses of the variables `pointX` and `pointY` (which belong to the `main` function and couldn't normally be accessed in `getUnitCirclePoint`). The function can then compute values and write them to these addresses (with dereference, `*x` and `*y`), changing the values in `pointX` and `pointY`, effectively returning two values.
|
||||
Function `getUnitCirclePoint` doesn't return any value in the strict sense, but thank to pointers it effectively returns two `float` values via its parameters `x` and `y`. These parameters are of the data type pointer to `float` (as there's `*` in front of them). When we call the function with `getUnitCirclePoint(i * 0.125 * 2 * PI,&pointX,&pointY);`, we hand over the addresses of the variables `pointX` and `pointY` (which belong to the `main` function and couldn't normally be accessed in `getUnitCirclePoint`). The function can then compute values and write them to these addresses (with dereference, `*x` and `*y`), changing the values in `pointX` and `pointY`, effectively returning two values.
|
||||
|
||||
Now let's take a look at pointers to structs. Everything basically works the same here, but there's one thing to know about, a [syntactic sugar](sugar.md) known as an arrow (`->`). Example:
|
||||
|
||||
|
@ -1477,7 +1477,7 @@ int main(void)
|
|||
}
|
||||
```
|
||||
|
||||
Notice that in `fopen` we now specify `"w"` (write) as a mode. Again, we check if the file has been opened successfully (`if (textFile != NULL)`). If so, we use a `while` loop to read and print all characters from the file until we encounter the end of file. The reading of file characters is done with the `fscanf` function inside the loop's condition -- there's nothing preventing us from doing this. `fscanf` again works the same as `scanf` (so it can read other types than only `char`s), just on files (its first argument is the file to read from). On encountering end of file `fscanf` returns a special value `EOF` (which is macro constant defined in the standard library). Again, we must close the file at the end with `fclose`.
|
||||
Notice that in `fopen` we now specify `"r"` (read) as a mode. Again, we check if the file has been opened successfully (`if (textFile != NULL)`). If so, we use a `while` loop to read and print all characters from the file until we encounter the end of file. The reading of file characters is done with the `fscanf` function inside the loop's condition -- there's nothing preventing us from doing this. `fscanf` again works the same as `scanf` (so it can read other types than only `char`s), just on files (its first argument is the file to read from). On encountering end of file `fscanf` returns a special value `EOF` (which is macro constant defined in the standard library). Again, we must close the file at the end with `fclose`.
|
||||
|
||||
We will now write to a binary file:
|
||||
|
||||
|
@ -1633,7 +1633,7 @@ int main(void)
|
|||
|
||||
puts("The string you entered backwards:");
|
||||
|
||||
while (charsRead > 0)
|
||||
while (charsRead > 1)
|
||||
{
|
||||
putchar(inputChars[charsRead - 1]);
|
||||
charsRead--;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue