Closed Thread Icon

Topic awaiting preservation: DS Link Pointer Probs Pages that link to <a href="https://ozoneasylum.com/backlink?for=8811" title="Pages that link to Topic awaiting preservation: DS Link Pointer Probs" rel="nofollow" >Topic awaiting preservation: DS Link Pointer Probs\

 
Author Thread
supreme_commander
Nervous Wreck (II) Inmate

From: QLD
Insane since: Mar 2003

posted posted 08-13-2003 06:49

The problem is that after the addapplicant() function has run and the program return to main i test it by printing the values in the structure but i get null for both. But before hand in the function i get whatever i put in. Can anyone help me. I believe it is a problem with pointers or soemthing

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

struct clientNode {
char *name;
char *address;
clientNode *link;
};


void addApplicant(clientNode *, clientNode *);

int main() {
clientNode *current;
clientNode *list;

addApplicant(current, list);

printf("%s\t%s", current->name, current->address);

}



void addApplicant(clientNode *current, clientNode *list) {
char nInput[30], aInput[40];

scanf(" %[^\n]s", nInput);
scanf(" %[^\n]s", aInput);

if (current == NULL) {
list = current = (clientNode*)malloc(sizeof(clientNode));

}
else {
current->link = (clientNode*)malloc(sizeof(clientNode));
current = current->link;
}

current->link = NULL;
current->name = (char*)malloc(strlen(nInput)+1);
strcpy(current->name, nInput);
current->address = (char*)malloc(strlen(aInput)+1);
strcpy(current->address, aInput);


printf("%s\t%s", current->name, current->address);

}



lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 08-13-2003 13:16

Hello,

This is a javascript board, however your problem in this c code might be because you're using:

scanf(" %[^\n]s", nInput);

instead of:
scanf(" %[^\n]", nInput);

(remove the 's')

regards,
elias

« BackwardsOnwards »

Show Forum Drop Down Menu