
|
Structs with functions and pointersstruct can be used with functions and pointers as well, just like any
other data types. You can pass in variables of a structure you define into functions,
and also have them as return values. Consider the example:
typedef struct {
int day;
int month;
int year;
} date;
date AddFiveDays(date original) {
original.day += 5;
// ... more stuff
return original;
}
int main() {
date today;
// ... set the date
today = AddFiveDays(today);
// ... do more stuff
}
Of course, since a defined struct is basically a new data type, you can
use pointers with them too. It will give you all the advantages of using pointers, such
as modifying variables from main inside function. Let's look at the following:
typedef struct {
int day;
int month;
int year;
} date;
void AddFiveDays(date* original) {
(*original).day += 5; // dereference original, and get to "day"
original->day += 5; // same as the above line!!
// ... more stuff
}
int main() {
date* thisWholeMonth;
thisWholeMonth = (date*)malloc(30*sizeof(date));
// ... set the date
AddFiveDays(today);
// ... do more stuff
}
Random numbers
Along with the simpio.h library, the author of your textbook also
included a random.h library that lets you make random numbers easily.
It is in the same path as the simpio.h library.
To use any kinds of random numbers in your program, first you must put
this at the beginning of your program:
Randomize();This function will call the srand() function in C and randomize the "seed" in your computer and make sure that your program doesn't get the same sequence of random numbers every time you run the program. After that, you have the following functions (from random.h):
int RandomInteger(int low, int high);This function generates a random integer. You pass in the range that you want the generated number to be in, and it returns a number within that range. double RandomReal(double low, double high);Same as above, but this one uses a double and not an integer. bool RandomChance(double p);Returns either TRUE or FALSE. You can pass in a double that is between 0 and 1, which is the probability of this being true. A higher value means more likely to return true. Input/output with Files
We'll have a quick tutorial on how to input/output with files in C. You will
be saving to and loading from a plain text file with this method. Let's see
the following example:
FILE* input;
input = fopen("myfile.txt", "r");
// ...
fclose(input);
This will read in a file called "myfile.txt". Next let's look at how to open a
file to write into it:
FILE* output;
output = fopen("myfile.txt", "w");
// ...
fclose(output);
The "w" in the file mode this time can be changed to "a" as well. "w" stands for
write and will overwrite whatever you have in the file (if it exists). "a" stands
for append and will append to the file if it already exists.Now to actually read from and write data into the file: In C there are many
functions like getc(), putc(), fgets() and a bunch of other stuff you can use.
But the most convenient one and the way I would recommend to do is the fprintf()
and fscanf() functions. The only difference is you also pass in your
FILE pointer to it to use it. For example:
fprintf(output, "%d", someInt);fscanf() is also similar to scanf(). If you wrote to your file a number like above, you would read it in like: fscanf(input, "%d", someInt);This is assuming your file only has one number in it (or at least as the first thing that appears in your file). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||