Wednesday, September 22, 2010

jeez

I keep banging my head over stupid stuff.

I finally got the program to do what I wanted (read a bit of header info), but man did the journey to that point make me feel dumb.

First of all, I will reiterate again that pointers aren't taught very well, and very few tutorials or books make sense of them. I swear right now that when I feel confident enough I will write a tutorial that would have worked for me 10 years ago.

The issue is that in small programs pointers do nothing for you. All tutorial code or code snippets in books are not using pointers like they would be used in real life.

Anyways, what I was banging my head over was pointers to structures.

If I have

struct woof {int i;};

and then I say

struct woof *dog;

it's analogous to saying

int *i;

and I forgot that. So I was doing this:

struct woof *dog;

dog->i = 5;

which you can't do, because although I declared a pointer to a structure woof, I never properly initialized the pointer by giving it a memory address to a legit instance (is instance the right word?) of the structure.

If I had said:

struct woof puppy;
struct woof *dog = &puppy;
dog->i = 4;

then I'd be good to go.

In a small program going through all that rigmarole is useless. I did it in my little program as practice, but I'm learning now that it's bad practice (despite learning a lot THIS time) because it's not "real world".

I think I'll be able to flesh out the program soon (when I package certain parts into functions) such that using pointers will be more efficient because I won't be passing unwieldy structures by value.

No comments:

Post a Comment