This has been driving me crazy for the past few hours. I'm attempting to take in two char pointers and a float as parameters. I then take the first 10 letters of the first char pointer and assign them as a char array to part of my linked list. I then do the same with the next char, however, if the first char is over 10 letters, the second is added to the first char.
So, if I enter me 234234234, the first char arrays are "me" and the second is "234234234".
If I enter morethantenletters 234234234, the first char is "morethantenletters234234234" and the second is "234234234". I have no idea why that gets concatenated with the first. With print statements I found out that the first char array gets screwed up after the second char array is put in the linked list. Anyway, heres the code:
Code:
struct node{
char name[10];
char acctno[9];
float balance;
struct node *next;
} node;
Code:
int BANKLOGadd(const char* name, const char* acctno, float balance) {
int i = 0;
struct node * newNode = malloc(sizeof(struct node));
for (i=0; i<10; i++){
newNode->name[i]=name[i];
}
/*always prints first char correctly*/
printf("%s\n", newNode->name);
for (i=0; i<9; i++){
newNode->acctno[i]=acctno[i];
}
/*always prints first char correctly unless it's over 10*/
printf("%s\n", newNode->name);
newNode->balance = balance;
newNode->next= head;
head = newNode;
printf("ADD %s %s %f\n", name, acctno, balance);
return 0;
}
Thanks in advance.