Sunday, October 31, 2010

get a line or two

Here is a function in the example program:

int getline(char s[], int lim)
{
int c, i;

i = 0;

while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;

if (c == '\n')
s[i++] = c;

s[i] = '\0';
return i;
}

Unfortunately the indentations are stripped out, but that's ok.

In the whole program there is a function for getting a line, and it's called inside a function for reading lines. Yo dawg...

It's important to know that the readlines function is waiting on i to be 0 so it knows it's read as much as it can. If getchar() encounters EOF it's going to skip over the parts where i gets incremented.

So getchar() is a function that has pissed me off since day one because it's so poorly elaborated on in any text I've found. It also has the peculiar property of being obviously Get Character and yet it returns values to an int (in the K&R examples, anyways). I believe that this is because of how throwing the returned value into a char would be interpreted once EOF is hit. I think it's just safer to use int... I'm just not sure why right now (although I recall reading this). Something about how EOF isn't the same in all operating systems, or how negative numbers are interpreted when they are char.

Anyways, getline() is passed a character array (more on this at the end) and a number from the readlines() function. The integer lim is supposed to be how long of a line we're allowing. If the line gets too long (or getchar() pulls in EOF or a newline) our while loop dies and AT THE LEAST s[0] is a string terminator.

I'm getting ahead of myself.

We have this loop, and this loop essentially starts slurping in characters from the input stream. When you execute the program that benign looking getchar() buried in this while condition inside of a function inside of a function makes the program just sit there with a blank terminal. You have to enter something (sentences) and hit enter (newline) a few times to get the ball rolling (since the program is about sorting sentences by length). In Windows, you send EOF with ctrl-z and hitting enter.

Alight, so getchar() LOOKS like it's getting whole character strings based off how you're entering them in the console, but it's not. That threw me for a while. It's only getting one character at a time. What I'm not certain about (again...not well documented anywhere) is if getchar() sees characters as I'm entering them, or only after I hit enter. I believe it's only after I hit enter, since I have to hit enter after sending EOF with ctrl-z.

So with that knowledge...

I type in a sentence and hit enter. getchar() starts churning through the input one character at a time and passes the value (ascii value?) as an integer to c. Every character that isn't EOF or a newline gets placed in an array s[]. A character array. Full of ints? Woof.

This is one of those fun tidbits I didn't know before I started this book, but there is a different between i++ and ++i. i++ evaluates i and THEN increments it. ++i increments first and then evaluates i. So when we're saying:

s[i++] = c;

we're saying TWO things. First, pass c into s[i], and then increment i. That was confusing to me for a while because when I was first learning you would make that two statements:

s[i] = c;
i = i + 1;

Moving on....

So we're throwing c in to s until we reach our limit or c is EOF or newline. Lets say c is EOF.

If c is EOF, the while loop ends, and the next if statement is skipped over (since we're not newline) and then s[i] becomes a the string terminating zero. Again remember back in the while loop we passed c to i and THEN incremented i, so once we bail out of our while loop we're already at the next i for placing our terminating zero. Cool beans.

If c is newline, then we go to the next if statement and I suppose check again if it's newline (I wonder if there is a better way to do that) and pass that newline into c, and then go to the next statement which adds the terminating zero.

Then the function returns how long the string is.

So. This function takes lines (defined by a string ended with a newline) from the input stream and puts them into an array with a proper terminating zero and then returns how long that array is. What I don't understand yet is why. Do those strings get kept? Sometimes pass by value and pass by reference still confuses me. Does my newly formed string s[] not get destroyed when the function is done because I passed where it's being stored by reference? At first glance it doesn't seem so, but I might be wrong. In the function calling this getline() function there is a character array:

char line[MAXLEN]

and getline() is called by:

getline(line, MAXLEN)

so does getline actually alter the char variable line[], or is it only working on s[]?

Tricky tricky. I could quickly write a program to test what happens (is line passed its value or reference), but I'm tired and I need to go to bed.

sorting

I typed out one of the programs I don't understand and got it to compile. It runs and takes input and gives output, but not what I expected. I think it's a program to sort lines of text by length, but it does it in such an obtuse way, and it doesn't work. It rearranges the lines I feed it, but not in any particular order.

I think I'll break it down here function-by-function to figure out how it works and possibly even fix what's broken.

Wednesday, October 27, 2010

off topic

Well, I didn't get any programming done today but I did further myself along in another long ignored hobby. I'll probably post the results of that effort when it's done.

I read about programming stuff more than I program, but reading other code and examples is keeping it fresh in my mind.

Monday, October 25, 2010

other things

I wish I'd have made this journal more general. There are other things I'm doing to better myself (cooking, for example) that I'd like to document. I've been keeping my notes in a document online, but this format would have done nicely as well.

Another topic I've struggled with my whole life is mathematics. I wonder what would happen if I tackled linear algebra or calculus on my own and documented my progress. It's worked so far with C programming. The only limitation seems to be time.

I'd also like to get in shape physically, learn how to play guitar beyond just chords, learn to play piano (again), and get a grip on analog circuitry (filters, power supplies, etc).

C comes first. It's something I have a lot of momentum behind right now and it's pretty relevant to my career (although more my future career plans than my current gig).

Calculus/Linear algebra might be a great companion project once I get over the C hump. Learning to program filters or doing statistics in code would definitely be a valuable tool.

Sunday, October 24, 2010

busy busy

This weekend was kind of a no-go on getting some coding done, but I had stuff to do.

Saturday, October 23, 2010

there is no spoon

Whoa, the chapter and example about self-referential structures (also known as linked lists, I think) is mind blowing. I'm glad I spent so long on understanding pointers because it all makes sense. My next project will be to write the example program in my own words.

This would require me having some free time. So.... could be a while.

Monday, October 18, 2010

the order

If I had to talk someone through learning C that had never been exposed to programming before, I'd probably make them go through something similar to how I grew up (although accelerated over months instead of 15 years).

1) Take an existing program with a graphical element in a non-complied language and modify bits of it. My first experience with a programming language was GW Basic and QBasic. I learned how to write loops to say things like "Chris is the best" over and over again on the screen. You can easily see in the code where the parts are for what is written on the screen, and how many times it loops. After that, I messed around with QBasic's Gorillas and Nibbles demos. It's pretty simple to find where the velocity and other parts are. Changing something that's already there and straightforward to run is an easy instant-gratification thing to do. I don't think that there are modern equivalents to this!

2) Dive into Pascal. This was the second language I got familiar with, and it introduced me to modern syntax, variables, advanced loops and other control elements, reading/writing files, objects, and doing more graphically advanced things on the screen. It's possible to compile Pascal, so that's a good step to learn.

3) Baby steps into C. My first experience with C was either Turbo C++ or DJGPP, I can't remember. I could do almost everything in C that I could do in Pascal except for reading/writing files and doing interesting things on the screen. DJGPP had the Allegro libraries ready to go, but this was where knowing how structures and pointers worked would have been useful. Ultimately I got frustrated and never advanced. So, I wouldn't recommend that path. I'm working on an order of learning for C that would have helped me when I was younger. The problem is the instant gratification element. Getting things on screen these days is difficult now that the console has been marginalized. Making little ASCII art characters walk around is prohibitively difficult - in fact I probably couldn't even do it now! It seemed so trivial in Basic and Pascal in hindsight.

K&R, as I've said before, is a great reference but poor tutorial. Not that it's trying to be one, but still. It really cheesed me off over the weekend with the structure padding thing. Structure padding seems so freaking important of a thing to know, but it gets one lousy sentence in K&R.

Sunday, October 17, 2010

sizeof, structures, and fread

I just read about structure padding, and now I'm worried.

If I have a file stream going and I want to shuttle in some data to a structure with fread(), will it go "too far" into the stream if I do this?

fread(&stuff, sizeof(struct putstuffhere), 1, filepointer);

What if my structure is an int and a char... it's technically five bytes, but sizeof will return eight! Will my file pointer location go three bytes too far into the stream if I try to read data into that five byte structure?

I can't find any info on this.

gtk+ installed

It took some doing, but I have the GTK+ packages going, and I was able to compile and run the sample program that Code Blocks defaults to.

Everything worked as I expected it to except for one thing: to run my program I had to copy a whole mess of .dll files from the GTK+ library. Does this mean that if I write a program I'd have to include a dozen .dll files in the root with the program itself? How do I tell it to maybe look in a different directory?

I need to find a small program that uses GTK+ that someone else did to see how they do it.

Anyways, big moment for me. I installed and compiled against a GUI toolkit. Not easy... but now I think I have a handle on how to do it to any arbitrary packaged library out there.

Monday, October 11, 2010

not too unproductive

I didn't get any programming done over the weekend, but I did indulge my other hobbies a bit.

Wednesday, October 6, 2010

someday

The more I read about GTK+, the more I want to learn it. It seems the most "C friendly" of the GUI libraries (when compared to QT anyways).

One day I'll have time for that. I think it will come after I figure out some of the esoteric examples in the K&R book, which has been on my to-do list for weeks now.

Tuesday, October 5, 2010

one down

I finished Mysterium. Only took two evenings. Wait... isn't this supposed to be my programming notes?

I've been giving thought to leaping forward a bit. I had promised myself that I wouldn't get ahead of myself and try to do things I didn't really understand, but I want to do more than manipulate strings and read from files. projecteuler.net might be a good start for ideas.

Monday, October 4, 2010

2.5

I started learning C on July 15th. So it's been about 2.5 months. I've made pretty good progress, I think. I had hoped to make another push at understanding structures over the weekend, but that never happened. I worked on Saturday, and spent Sunday evening in The Colony because my Dad was in town. Everything in between was a frantic catchup on chores. I did take a break to go to Half-Priced Books and get $10 for a big box of textbooks I salvaged from my parent's house. Most of the $10 went to buying A Stranger in a Strange Land by Robert Heinlein, and Mysterium by Robert Charles Wilson. I started Mysterium. It's good so far - it might make me track down more "alternate history" kind of books.