CSC 209: Assignment 2 FAQ
Here are some answers to commonly asked (or should be commonly asked)
questions, along with some hints for getting your code to work.
Part 0
- How do I get a memory address for each of the areas in memory?
Cause some memory to be allocated in the segment, and use the address
operator (&) or print the value of a pointer.
- How do I print the value of a pointer?
With one of the printf conversions. There's one specifically
for pointers.
- How do I get the man page for the printf function?
man printf gives a man page for a system command!
It's in chapter 3 of the manual, so use man 3 printf.
- How can I get an address in the code segment?
Code is stored on the code segment. The name of a function is actually
a pointer to the function, just like the name of an array is actually a
pointer to the array. (We'll talk more about function pointers later in
the course.)
- What is stored in the ______ segment?
Consider where local, global and static variables are stored, and where
dynamically allocated memory is allocated.
Part 1
- Do I have to read input into an array of characters?
Yes! fgets does not allocate any space, so you have to allocate
it yourself, either by declaring an array of characters or using malloc.
- I want to get a pointer to the i'th TAB character in a string
I read with fgets. How do I do it without duplicating code?
strchr gives you a pointer to the first occurrence of a
character in the string. If you want a pointer to the second, that's
equivalent to calling strchr on the string returned by
strchr (plus 1). A loop will make your code look pretty.
- How do I convert a string representing an integer (such as in a byte list)
to an actual integer?
Consider the atoi function.
- How do I convert an integer to a string representation?
Well, printf does it for output, so look at its related
function named sprintf that "prints formated output to a string."
- What is the maximum length of a line of input?
I don't know. Get it working with a very large maximum length first
(maybe a few thousand characters) before worrying about getting it to work
on arbitrary lengths (arbitrary line length isn't needed for this assignment,
but you should think about how you could do it).
Part 2