Other Important Things
Things that are useful, but wont be explained too in-depth. These are still important but either they're seldomly used or we'll be going over them in lecture and recitations. I'll leave it to you guys to figure out how to use these (man pages, tutorialspoint, any other documentation available for free online, etc.).
Pointer Arithmetic
Pointer arithmetic isn't something you'll be using often, but again, you should know it since it has its uses.
Pointer arithmetic is simple. You take a pointer... and you add to it (or any operation really).
What use does this have? Didn't we establish earlier that we don't want to stray from the address a pointer is referring to? The answer is (mainly) arrays (and getting virtual addresses of page tables but you'll get to that in CS416 eventually so don't worry about it for now).
#include<stdio.h>
int main(...){
int a[5] = {1, 2, 3, 4, 5};
print("%d\n", a+3);
return 0;
}
This will print 4 since a is pointing to the start of the array and we accessed an offset of 3. Something to remember is that when
doing pointer arithmetic, the number of bytes you jump is based on type. So when you do a+3, you're really doing
address_at_0 + (3*sizeof(int) bytes).
This might be particularly useful when manipulating strings.
Ternary statements
Extremely convenient for shortening simpler conditionals. The syntax is the same as Java.
read(), write(), dirent, and other File Based I/O
For the most part, you won't be needing these until the last project. We'll be going over them later on in the semester if you need a refresher, so don't worry too much about these. If you want to learn them now you can check the man pages or tutorialspoint's C tutorial on all of these things.
memcpy(), memset(), calloc()
Rather than initializing every member of a struct manually, if you just need everything to be NULL or zeroed out, you can just use memset(). Alternatively, you can use calloc() which is similar to using malloc() then memset().
Realloc
Reallocating space that you already requested (usually for resizing). Similar to
malloc()new ->memcpy()old ->free()old.
Be careful when using this. While it's convenient, it isn't the solution to everything.
Also, regardless of whether you malloc() to reallocate space or realloc(), never reallocate 1 byte at a time unless you really want
to segment your heap and slow down your program. As a general tip, if you don't know how much space you need, start off with a reasonable
guess. If you need to reallocate and you don't know how much extra space you need, double your allocation (or make another reasonable
guess and go slightly over that), then resize once you know close to the amount you need. Don't do this too often otherwise your program
will spend more time reallocating than it needs to.
Const
Similar to final in Java, but does different things based on where it's placed in a variable declaration.
Static
This might be a little confusing at first (it doesn't do the same thing as static in Java), but in short, if it's used on a global,
the global is
- initialized to zero and
- its value persists throughout the lifetime of the program
Particularly useful when the
global is in a different file from main().
If it's used on a function, it makes the function visible only to the current source file
(similar to private in Java, though not quite).
I'll leave it to you to figure out what it does if you use it locally in a function, but you won't be seeing this usage in this class.