Article View: pl.comp.lang.c
Article #1101Re: Wzajemna referencja struktur. (poprawka)
From: Robert Wiejak
Date: Thu, 29 Jan 1998 18:56
Date: Thu, 29 Jan 1998 18:56
67 lines
2103 bytes
2103 bytes
Robert Wiejak wrote in message <6aqfn1$fn8$1@news.bctel.net>...
>
>Adam Bienias wrote in message <34D0AA82.46D7@polbox.com.pl>...
>>Mam problem ktorego jakos nie moge rozwiazac. Potrzebuje zdefiniowac
>>dwie struktury, przy czym kazda moze wskazywac na druga:
>>
>>typedef struct {
>> BETA_STRC *hans;
>>} ALFA_STRC;
>>
>>typedef struct {
>> ALFA_STRC *kloss;
>>} BETA_STRC;
>>
>>Jak to zrobic ?
>
>Troche twoj przyklad zmienilem, ale to tylko zeby latwiej
>zrozumiec:
>
Zmienilem tu struct. ALFA_STRC i BETA_STRC.
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct ALFA_STRC
{
struct BETA_STRC *beta;
char imie[25];
} ALFA_STRC;
typedef struct BETA_STRC
{
struct ALFA_STRC *alfa;
char nazwisko[25];
} BETA_STRC;
main(void)
{
ALFA_STRC *alfa = NULL;
BETA_STRC *beta = NULL;
alfa = (ALFA_STRC*) malloc(sizeof(ALFA_STRC));
beta = (BETA_STRC*) malloc(sizeof(BETA_STRC));
alfa->beta = beta;
beta->alfa = alfa;
// pisze do BETA_STRC przez ALFA_STRC
strcpy(alfa->beta->nazwisko, "Kloss");
// pisze do ALFA_STRC przez BETA_STRC
strcpy(beta->alfa->imie, "Hans");
// bezposrednio
printf("Nazwisko: %s\n", beta->nazwisko);
printf(" Imie: %s\n\n", alfa->imie);
free(alfa);
free(beta);
return 0;
}
Robert
Technical Details
Message-ID:
<6aqibd$9jm$1@newsfeed.smartt.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!newsfeed.direct.ca!news.bctel.net!newsfeed.smartt.com!not-for-mailReferences:
<34D0AA82.46D7@polbox.com.pl> <6aqfn1$fn8$1@news.bctel.net>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!newsfeed.direct.ca!news.bctel.net!newsfeed.smartt.com!not-for-mail From: "Robert Wiejak" <hbg@xptcan.com> Newsgroups: pl.comp.lang.c Subject: Re: Wzajemna referencja struktur. (poprawka) Date: Thu, 29 Jan 1998 10:56:14 -0800 Organization: Optio Lines: 69 Message-ID: <6aqibd$9jm$1@newsfeed.smartt.com> References: <34D0AA82.46D7@polbox.com.pl> <6aqfn1$fn8$1@news.bctel.net> NNTP-Posting-Host: 207.102.114.8 X-Newsreader: Microsoft Outlook Express 4.71.1712.3 X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit