Welcome back.
Data Swapping
After a brief hiatus due to some copywriting concerns, I am happy to say I am back on track with CS50. I’ll be finishing Week 4 and moving on to Week 5 today, which is titled ‘Data Structures.’ Exciting new concepts and code to follow, I am sure!
Either way, the task at hand is currently data swapping within C.
It’s a fairly simple concept, but one that allows you to directly address and draw from different points of data from different functions and other parts of your program.
The example given in the CS50 course is like having two cups, one filled with grape juice and the other with water. The cups are variables, and the liquids are the values of those variables.
If you want to switch them, you can’t pour one into the other directly, as that would just combine the liquids, not necessarily swap them. Therefore, you need an empty third cup that can serve as a temporary data ‘point’ by which the water can enter. The grape juice can occupy the water cup, and the cup in the temporary ‘point’ can empty its contents into the grape juice cup. Now, at the end of the function, the temporary cup is again empty, but the grape juice and the water are now in each other’s cup without having made direct contact with one another, and without getting mixed up.
That’s a little silly, and this is programming, so let’s actually demonstrate this concept using code.
#include <cs50.h>
#include <stdio.h>
void swap (int *a, int *b);
int main(void)
{
int x = 1;
int y = 2;
printf("x is %i, y is %i\n", x, y);
swap (&x, &y);
printf("x is %i, y is %i\n", x, y);
}
void swap (int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
Now, I am declaring the swap function at the top of the code. I assign x and y their values respectively, then write the basic printf function, which is really just for the user.
The line swap (&x, &y); is where it gets interesting, though, as the ampersand passes the address of x and y to the swap function, and the asterisk gets the values that we’re pointing to using the *a = *b; etc., line. The tmp line assigns the new value, the program runs and printf is executed, and voila, the values have been swapped.
x—x
Dictionary Entries:
heap overflow: when you overflow the heap and touch memory that you shouldn’t touch ‘up top’
stack overflow: when you overflow the stack and touch memory that you shouldn’t touch ‘down there’
These are both types of buffer overflows, which is what happens when you have bugs in your code and data gets touched inappropriately.
Creating, Opening, And Copying To Files
Fortunately, creating and copying to files is very easy in C.
Let’s make a program in C that allows us to store information in a file, as opposed to just on the hard drive.
First, I’ll obviously want to code phonebook2.c and code phonebook2.csv.
Here’s the program:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *file = fopen("phonebook2.csv", "a");
string name = get_string("Name: ");
string number = get_string("Number: ");
fprintf(file, "%s, %s\n", name, number);
fclose(file);
}
This program will allow me to prompt the user for a name and number as in lines 7 and 8, then the fprintf file will print the value of the user’s input to the phonebook file as specified in line 6, phonebook2.csv.
I can print as much data as I would like to this ‘spreadsheet.’
x—x
Thanks for reading!