Where do I implement that? In helpers.c? helpers.h?
A library's .h
file is a header file. It does NOT contain definitions. Instead, it contains code that declares the library's resources that are available for you to use. These resources include functions, variables, structs, type definitions, etc (these, together, are commonly referred to as the interface).
A library's .c
file is an implementation file. It, on the other hand, is the one that contains the definitions (aka implementations).
Usually, header files and implementation files go hand in hand. So if you've declared a bunch of string functions in mystring.h
, you define these functions in mystring.c
.
Why are they giving away the selection sort instead of letting us figure it out?
Well, the sections are optional to watch. You could have skipped watching them if you want to. Another good idea is that you could have also paused the video and tried to figure that out by yourself! Or skipped the part in which they solve the problem and watched the rest of the section. There were many solutions! :)
In David's "Noswap" video, he says that you can't just swap variables around.
I'm sure the professor didn't say that because surely we can. I believe in the world of computer science and programming, everything is possible.
As far as I remember, what the professor said is that you can't swap variables in a specific case. This case is in which you pass these variables by values to a function. We'll get to that in a minute, but please consider the following function
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
This simple function is supposed to be swapping two variables -- a
and b
. However, when we use this function
int x = 10;
int y = 20;
printf("Before swapping:\nx: %d\ny: %d\n\n", x, y);
swap(x, y);
printf("After swapping:\nx: %d\ny: %d\n", x, y);
Output:
Before swapping:
x: 10
y: 20
After swapping:
x: 10
y: 20
We clearly don't see any swapping at all. So what happened?
Well, when we passed x
and y
to swap()
, we actually passed copies of their values. These copies are stored in a
and b
(swap()
's parameters). We then swap the values in a
and b
(the copies) and nothing really happens to the original values in x
and y
. That's why we don't see any swapping.
But how can we fix that?
We simply code swap like this
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
Now, when we use the new version of swap()
int x = 10;
int y = 20;
printf("Before swapping:\nx: %d\ny: %d\n\n", x, y);
swap(x, y);
printf("After swapping:\nx: %d\ny: %d\n", x, y);
Output:
Before swapping:
x: 10
y: 20
After swapping:
x: 20
y: 10
Okay what happened here?
Well, these little asterisks (i.e., *
) have a meaning (you'll know more about them when you get to pointers). But, for the sake of simplicity, this way, we didn't really pass copies of the values in x
and y
, but instead, we passed their addresses of the original values in memory -- where they live in memory. We then coded swap()
in such a way to go to these addresses and play with the values that live there. And that explains why we can see swapping this time.