C/Napisy - własna implementacja: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Nie podano opisu zmian
komentarze w kodzie źródłowym
Linia 18:
Na początek zajmiemy się wypisywaniem łańcucha:
<source lang="c">
static string str;
static struct string_data
{
char c;
struct string_data *next;
};
typedef struct _string
{
FILE *(*wypisz)(FILE*); /* wskaźnik na metodę */
} *string;
FILE *wypisz_str(FILE *strum) /* metoda */
{
string wsk = str;
for(;wsk != NULL;wsk = wsk->next) /* pętla po znakach */
fputc(strum, wsk->c); /* wypisz znak */
}
</source>
Teraz zajmijmy się przypisaniem:
<source lang="c">
static string str;
typedef struct _string
{ char c;
Linia 44 ⟶ 37:
void (*wpisz)(const char*);
} *string;
static string str;
void wpisz_do_str(const char *new_string)
{
Linia 62 ⟶ 56:
{
string new_string = malloc(sizeof *new_string);
new_string->wypisz = wypisz_str; /* ustawienie wskaźników na metody */
new_string->wpisz = wpisz_do_str;
for(;initial && *initial;++initial) /* inicjalizacja */
{
struct _string new_char;
Linia 71 ⟶ 65:
tmp->next = new_char;
}
str = new_string; /* ustawienie adresu ostatniego napisu */
return new_string;
}
Linia 79 ⟶ 74:
if((*s)->next)free_string(s->next);
free(*s);
*sstr = NULL;
}
</source>