I am creating a linked list and trying to add a set of data to the last one but i cant get the function to alter the last pointer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
char data[10];
struct node *nxtP;
}node;
void addF(node *hdr, char data[10]){
node *temp;
temp = (node*)malloc(sizeof(node));
strcpy(temp->data, data);
temp->nxtP = NULL;
*hdr = *temp;
}
void addR(node **hdr, char data[10]){
node *temp;
node *newN;
node *temp2;
temp = (node*)malloc(sizeof(node));
temp2 = (node*)malloc(sizeof(node));
newN = (node*)malloc(sizeof(node));
*temp = **hdr;
strcpy(newN->data, data);
temp->nxtP = newN;
printf("%p - ", newN);
printf("%p - ", temp->nxtP);
}
int main(){
node *hdr;
node *temp;
node *temp2;
char data[10];
int k;
hdr = (node*)malloc(sizeof(node));
temp = (node*)malloc(sizeof(node));
temp2 = (node*)malloc(sizeof(node));
scanf("%s", data);
addF(hdr, data);
scanf("%s", data);
addR(&hdr, data);
printf("%p\n", hdr->nxtP);
for(*temp2 = *hdr; temp2 != NULL; temp2 = temp2->nxtP)
printf("%s\n", temp2->data);
free(hdr);
free(temp2);
return 0;
}
the adresses in the function tell me that they are not altering the hdr->nxtP part soo what am i doing wrong???
thanx

New Topic/Question
Reply



MultiQuote





|