delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/05/12:32:12

Reply-To: <arfa AT clara DOT net>
From: "Arthur" <arfa AT clara DOT net>
To: "DJGPP Mailing List" <djgpp AT delorie DOT com>,
"Markus Ewald" <Markus_Ewald AT gmx DOT net>
Subject: RE: how do i define structures ?
Date: Sat, 5 Sep 1998 17:31:42 +0100
Message-ID: <000101bdd8ea$aa4d9540$5d4d08c3@arthur>
MIME-Version: 1.0
Importance: Normal
In-Reply-To: <35EFEEF9.F9BF8861@gmx.net>

>I don't exactly know how to use structures. If I define a structure
>typedef struct mystruc

>  int x;
>  int y;
>} mystruc;

Do you actually need the last mystruct? Perhaps I've been using C++ for too
long (where you don't even need typedef), but I thought that you could
define a typedef'd structure thus:

typedef structs mystruct {
    int x;
    int y;
}

>at the beginning i used it this way:
>int main(void)

>  mystruc pos1;
>  pos1.x=10;
>  pos1.y=20;
>}

>i wondered long time how to have functions return a structure. after i got
>djgpp and allegro, i looked at the sourcecode of allegro and saw that
they're >using pointers
>
>int main(void)

>  mystruc *pos1=malloc(sizeof(mystruc));
>  pos1->x=10;
>  pos1->y=10;
>  free(pos1);
>}
>
>this way it is possible to return a structure in a function. My problem:
>Do I have to use malloc and reserve space for the structure in order to use
>it first ? It worked fine without malloc-ing, but in the worst case, i can
be >happy
>that it didn't crash because I've overwritten critical memory...

The point of structures is that the entire structure acts like a single
variable. Just like an int is (in DJGPP) an area of memory which contains 4
bytes, a mystruct is an area of memory which contains two ints. Although the
way you use these is different, the way you use pointers with them is
exactly the same.

If you are unsure on pointers, check out
http://www.chesworth.com/pv/vault/vault.htm because it has the best tutorial
on pointers (including using pointers with structs) that I have seen.

Oh, and please don't send messages in HTML/RTF format in future. Thankyou.

James Arthur
jaa AT arfa DOT clara DOT net
ICQ#15054819

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019