Ansi-C struct og pointer problemer.
Jeg sidder og roder lidt med pointere i Ansi-C, derfor har jeg dette program som bliver ved med at "crashe" pga mine pointere der er udkommenteret i min swap funktion. Kan du hjælpe mig ? hvad gør jeg galt ?Min kode:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define STR_AF_LISTE 4
struct node
{
int data;
struct node *pointer_left;
struct node *pointer_right;
};
void show( struct node *pointer);
void swap_( struct node *pointer1, struct node *pointer2);
void main()
{
int i, j;
int inputarray[STR_AF_LISTE] = {45, 32, 2, 36};
struct node datanode[STR_AF_LISTE];
struct node *pointer_firstnode = NULL;
struct node *pointer_lastnode = NULL;
struct node *pointer_temp = NULL;
/* pointer brugt til show funktion */
struct node *pointer_show = NULL;
/* pointer brugt til swap funktion */
struct node *p1 = NULL;
struct node *p2 = NULL;
/* initialiser liste */
for ( i=0; i<STR_AF_LISTE; i++)
{
if ( i == 0 )
{
datanode[i].pointer_left = NULL;
datanode[i].data = inputarray[i];
datanode[i].pointer_right = &datanode[i+1];
}
else
{
if (i == (STR_AF_LISTE -1))
{
datanode[i].pointer_right = NULL;
}
else
{
datanode[i].pointer_right = &datanode[i+1];
}
datanode[i].pointer_left = &datanode[i-1];
datanode[i].data = inputarray[i];
}
}
/* udskriv liste */
for (i=0; i<STR_AF_LISTE; i++)
{
printf("\n[%i] data = %i", i, datanode[i].data);
printf(" adr [%i] = %i ", i, &datanode[i]);
printf(" [%i] left = %i", i, datanode[i].pointer_left);
printf(" [%i] right = %i", i, datanode[i].pointer_right);
}
/* sæt pointere til at pege på første og sidste */
pointer_firstnode = &datanode[i];
for (i=0; i<STR_AF_LISTE; i++)
{
pointer_lastnode = &datanode[i];
}
printf("\n\n\nTilgang vha first og last pointer:\n");
printf("\nFirst data = %d", pointer_firstnode->data);
printf("\nLast data = %d", pointer_lastnode->data);
pointer_temp = &datanode[i];
printf("\npointer temp data = %i > ", pointer_temp->data);
pointer_temp = pointer_temp->pointer_right;
pointer_show = &datanode[3];
printf("\nShow pointer -> %d", pointer_show->data);
show(pointer_show);
p1 = &datanode[1];
p2 = &datanode[2];
swap_(p1, p2);
for (i=0; i<STR_AF_LISTE; i++)
{
printf("\n[%i] data = %i", i, datanode[i].data);
printf(" adr [%i] = %i ", i, &datanode[i]);
printf(" [%i] left = %i", i, datanode[i].pointer_left);
printf(" [%i] right = %i", i, datanode[i].pointer_right);
}
getch();
}
void show( struct node *pointer)
{
printf("\nShow function -> %d", pointer->pointer_right);
while (pointer->pointer_right != NULL)
{
printf("\nData = %d right = %d left = %d", pointer->data, pointer->pointer_right, pointer->pointer_left);
pointer++;
}
}
void swap_( struct node *pointer1, struct node *pointer2)
{
struct node *ptemp = NULL;
printf("\nSwap p1 = %d", pointer1->data);
printf(" left = %d", pointer1->pointer_left);
printf(" right = %d", pointer1->pointer_right);
printf("\nSwap p2 = %d", pointer2->data);
printf(" left = %d", pointer2->pointer_left);
printf(" right = %d", pointer2->pointer_right);
/* compileren crasher hvis jeg bruger nedestående */
/*
ptemp->pointer_left = pointer1->pointer_left;
ptemp->pointer_right = pointer1->pointer_right;
pointer1->pointer_left = pointer2->pointer_left;
pointer1->pointer_right = pointer2->pointer_right;
pointer2->pointer_left = ptemp->pointer_left;
pointer2->pointer_right = ptemp->pointer_right;
*/
printf("\n\nSwap udført");
printf("\nSwap p1 = %d", pointer1->data);
printf(" left = %d", pointer1->pointer_left);
printf(" right = %d", pointer1->pointer_right);
printf("\nSwap p2 = %d", pointer2->data);
printf(" left = %d", pointer2->pointer_left);
printf(" right = %d", pointer2->pointer_right);
}