All pastes #64769 Raw Edit

Untitled

public c v1 · immutable
#64769 ·published 2006-06-12 09:13 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct pistacchio {	int primo;	int secondo;	double terzo;} pistacchio;struct node {	void *item;	unsigned int item_size;	unsigned int ID;	struct node *next;};typedef struct node *Node;void *m_alloc(unsigned int n, unsigned int m){	Node tmp;	tmp = (void *)malloc(n);	if (tmp == NULL) {		fprintf(stderr, "ERRORE: memoria insufficiente per struttura nodo\n");		exit (1);	}	tmp->item = (void *)malloc(m);	if (tmp->item == NULL) {		fprintf(stderr, "ERRORE: memoria insufficiente per item\n");		exit (1);	}	return (tmp);}void add_nodo(Node *head, Node *toadd) {	Node tmp;	tmp=*head;		if (tmp==NULL) {		*head=*toadd;		return;	}		while (tmp->next!=NULL)		tmp=tmp->next;	tmp->next=*toadd;		// possibilità di variante con restituzione testa lista	return;}void print_lista(Node lista){	Node tmp;		tmp=lista;	while (tmp!=NULL){		printf("%d\n",tmp->ID);		tmp=tmp->next;	}	return;}int main(void) {	Node lista=NULL, tmp=NULL;	pistacchio testdato;	int i,j;		testdato.primo=1;	testdato.secondo=2;	testdato.terzo=3.0f;		printf("%d\n", sizeof(struct node));		// crea il nodo allocando struttura e item	tmp=(Node)m_alloc(sizeof(struct node), sizeof(pistacchio));	tmp->ID=1;	tmp->next=NULL;	// riempi il nodo ---> per tipi di dato semplici basta fare assegnazione	// per tipi di dato complessi o già in memoria, conviene copiare da mem a mem	// praticamente c'è la stessa differenza che in C++ intercorre	// tra costruttori di classe e costruttori per copia	tmp->item=memcpy(tmp->item, &testdato, sizeof(pistacchio));		// aggiungi il nodo in coda alla lista	add_nodo(&lista, &tmp);		testdato.primo=4;	testdato.secondo=5;	testdato.terzo=6.0f;	tmp=(Node)m_alloc(sizeof(struct node), sizeof(pistacchio));	tmp->ID=2;	tmp->next=NULL;	tmp->item=memcpy(tmp->item, &testdato, sizeof(pistacchio));	add_nodo(&lista, &tmp);		print_lista(lista);	}