--- bootstrap <[EMAIL PROTECTED]> wrote:
>
> Can you explain what is the purpose of the
> symbol before the opening brace? I have seen that symbol in
> about a zillion places, and wonder what is its purpose. Also,
> I guess I was always confused into thinking the symbol after
> the closing brace was an instance/variable of the structure
> declared between the braces, and type-named before the
> opening brace. Though I have never been totally clear about
> the exact significance of the symbols before and after...
>
When using just struct, the type's name must come before the opening
brace, as after the closing brace come the instances' names.
When using typedef struct, the new type's name must come after the old
type definition, so it must come after the closing brace. The name that you
may find after typedef struct but before the opening brace is the struct's
name, that is the old type name. The struct's name may be useful for a
typedef struct to reference another of its kind. Read the following example:
#include <stdio.h>
#include <stdlib.h>
typedef struct _person {
char *name;
struct _person* other;
} person;
void print_linked_list (person *p) {
while (p) {
printf ("%s\n", p->name);
p = p->other;
} // while
} // void print_linked_list (person *p)
int main (int argc, char **argv) {
person *first = NULL;
person *last = NULL;
int i=1;
for (; i<argc; i++) {
person *p = malloc (sizeof(person));
if (!p) {perror ("malloc"); return EXIT_FAILURE;}
p->name = argv[i];
p->other = NULL;
if (!first) first=p;
if (last) {last->other=p;}
last=p;
} // for
print_linked_list (first);
return 0;
} // main
Abra sua conta no Yahoo! Mail, o único sem limite de espaço para
armazenamento!
http://br.mail.yahoo.com/