🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.c
Article #1100

Re: Wzajemna referencja struktur.

#1100
From: Robert Wiejak
Date: Thu, 29 Jan 1998 17:56
63 lines
1956 bytes
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:

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct
{
   void  *beta;
   char imie[25];
} ALFA_STRC;

typedef struct
{
   void  *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  = (void*)beta;
    beta->alfa = (void*)alfa;

    // pisze do BETA_STRC przez ALFA_STRC
    strcpy(((BETA_STRC*)alfa->beta)->nazwisko, "Kloss");

    // pisze do ALFA_STRC przez BETA_STRC
    strcpy(((ALFA_STRC*)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: <6aqfn1$fn8$1@news.bctel.net>
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!not-for-mail
References: <34D0AA82.46D7@polbox.com.pl>

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!not-for-mail
From: "Robert Wiejak" <a1a81825@bc.sympatico.ca>
Newsgroups: pl.comp.lang.c
Subject: Re: Wzajemna referencja struktur.
Date: Thu, 29 Jan 1998 09:56:42 -0800
Organization: BCTEL Advanced Communications
Lines: 65
Message-ID: <6aqfn1$fn8$1@news.bctel.net>
References: <34D0AA82.46D7@polbox.com.pl>
NNTP-Posting-Host: a1a81825.sympatico.bconnected.net
X-Newsreader: Microsoft Outlook Express 4.72.2106.4
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit