| << Prev | Beej's Guide to C | Next >> |
You've been messing with variables for quite some time now, and you've made a bunch of them here and there to do stuff, yes? Usually you bundle them into the same basic block and let it go at that.
Sometimes, though, it makes more sense to put them together into a
structure. This is a construct that allows you to logically
(or even illogically, if the fancy takes you) group variables into, uh,
groups. You can then reference the group as a whole. One place this
comes in really handily is if you want to pass 13 bazillion parameters
to a function, but don't want the function declaration to be that long.
You just put all the variables into a structure (or
So how do you use one of these things? First off, the
Adding to the confusion, it turns out there are multiple ways to
create and use a new
#include <stdio.h>
/* Here we declare the type so we can use it later: */
struct stuff {
int val;
float b;
};
/* Note that we don't actually have any variables of that type, yet. */
int main(void)
{
/* ok, now let's declare a variable "s" of type "struct stuff" */
struct stuff s;
s.val = 3490; /* assignment into a struct! */
s.b = 3.14159;
printf("The val field in s is: %d\n", s.val);
return 0;
}
The compiler allows us to predeclare a
Now let's talk a bit about how to pass these
Don't you hate it when the professor asks you a question like that, but you're too tired in lecture to care to begin to think about it? "Just tell me and we'll get on with it," is what you're thinking, isn't it.
Fine! Be that way! Well, remember that when you pass parameters to
functions, and I'll clear my throat here in preparation to say again,
EVERY PARAMETER WITHOUT FAIL GETS COPIED ONTO THE STACK when you
call a function! So if you have a huge
Instead, why not pass a pointer to the
And there's even a little bit of syntactic sugar to help
access the fields in a pointer to a
i = i + 12; /* add 12 to i */ i += 12; /* <-- does exactly the same thing as "i = i + 12" */
You see? It's not a necessary operator because there's another way to do the same thing, but people like the shortcut.
But we are way off course, buster. I was talking about pointers to
#include <stdio.h>
/* Here we declare the type so we can use it later: */
struct antelope {
int val;
float something;
};
int main(void)
{
struct antelope a;
struct antelope *b; /* this is a pointer to a struct antelope */
b = &a; /* let's point b at a for laughs and giggles */
a.val = 3490; /* normal struct usage, as we've already seen */
/* since b is a pointer, we have to dereference it before we can */
/* use it: */
(*b).val = 3491;
/* but that looks kinda bad, so let's do the exact same thing */
/* except this time we'll use the "arrow operator", which is a */
/* bit of syntactic sugar: */
b->val = 3491; /* EXACTLY the same as (*b).val = 3491; */
return 0;
}
So here we've seen a couple things. For one, we have the manner with
which we dereference a pointer to a
But, syntactic sugar to the rescue, we can use the arrow
operator (->) instead of the dot operator!
Saves us from looking ugly in the code, doesn't? The arrow operator has
a build-in deference, so you don't have to mess with that syntax when
you have a pointer to a
This is going to be a very short section about passing pointers to
structs to functions. I mean, you already know how to pass variables as
paramters to functions, and you already know how to make and use
pointers to
Grrrr. Why do I always sit on this side of the bus? I mean, I know that the sun streams in here, and I can barely read my screen with the bright light blazing away against it. You think I'd learn by now. So anyway...
#include <stdio.h>
struct mutantfrog {
int num_legs;
int num_eyes;
};
void build_beejs_frog(struct mutantfrog *f)
{
f->num_legs = 10;
f->num_eyes = 1;
}
int main(void)
{
struct mutantfrog rudolph;
build_beejs_frog(&rudolph); /* passing a pointer to the struct */
printf("leg count: %d\n", rudolph.num_legs); /* prints "10" */
printf("eye count: %d\n", rudolph.num_eyes); /* prints "1" */
return 0;
}
Another thing to notice here: if we passed the
Just a quick note here to get you thinking about how you're going to
be breaking stuff down into basic blocks. You now have a lot of
different tools at your disposal: loops, conditionals,
| << Prev | Beej's Guide to C | Next >> |