Δημοσιεύτηκε: 21 Ιούλ 2011, 14:17
από stamatiou
migf1 έγραψε:
Κώδικας: Επιλογή όλων

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

typedef struct node {
int data;
struct node *next;
} Node;

typedef struct list {
Node *head;
} List;

// -------------------------------------
void head_assign_byValue( Node *head, Node *p )
{
head = p;
return;
}

// -------------------------------------
void head_assign_byReference( Node **head, Node *p )
{
*head = p;
return;
}

// -------------------------------------
int main( void )
{
List list;
Node *p;

// create a node for p
p = calloc( 1, sizeof(Node) );
p->data = 100;
p->next = NULL;

// create a node for list.head
list.head = calloc( 1, sizeof(Node) );
list.head->data = 0;
list.head->next = NULL;

// try to make list.head pointing to p, using pass by value
printf("list.head->data before head_assign_byValue(): %d\n", list.head->data);
head_assign_byValue( list.head, p );
printf("list.head->data after head_assign_byValue(): %d\n\n", list.head->data);

// try to make list.head pointing to p, using pass by reference
printf("list.head->data before head_assign_byReference(): %d\n", list.head->data);
head_assign_byReference( &(list.head), p );
printf("list.head->data after head_assign_byReference(): %d\n", list.head->data);

free( p );
free( list.head );

exit( EXIT_SUCCESS );
}


Χωρίς παρεξήγηση αλλά μου φάνηκε αρκετά εύκολο! :eh: