Originally Posted by
harmony
I'm writing in c rather than c++.
Yes, and hence my code snippet is in C, not C++. What made you think that C++ is involved?
Originally Posted by
harmony
The reason I put the debugger output here was because I've tried posting in other places and got told to use a debugger, so of course everyone's sending me round in ceicles when it's probably only a couble of lines of the code I probably need to correct.
It is hard to even read your code because you did not format it properly. For example:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*The structure to store the data and the pointer to the next item in the list.*/
typedef struct item {
char *name;
char *shelf;
int stock;
double price;
struct item *next;
} item;
item *addtoend(item *start, item *current) {
char *input = malloc(25);
printf("Enter data separated by tabs.\n");
/*Get the user input then parse it into the separate data pieces.*/
fgets(input, 25, stdin);
sscanf(input, "%s\t%s\t%d\t%lf", current->name, current->shelf, ¤t->stock, ¤t->price);
free(input);
/*If the list is empty set the start pointer to the current pointe and start's next pointer to NULL. Otherwise set current's next pointer to start.*/
if (start == NULL) {
start = current;
start->next = NULL;
} else {
current->next = start;
}
return current;
}
int main() {
/*The start and current pointers point to the start of the list and the current item respectively.*/
item *start = malloc(sizeof(item)), *current = malloc(sizeof(item));
char *command = malloc(15);
printf("welcome to the shop.\n");
/*Check that the program really did get the memory.*/
if (start != NULL) {
while (fgets(command, 15, stdin)) {
if (strncmp(command, "addtoend", 8) == 0) {
addtoend(start, current);
printf("%s added to the %s shelf.\n", current->name, current->shelf);
} else if (strncmp(command, "quit", 4) == 0) {
/*Free the pointers that were dynamically allocated memory earlier.*/
free(start);
free(current);
free(command);
printf("Exiting ...\n");
break;
}
}
} else {
printf("Memory could not be allocated!\n");
}
printf("Thank you for visiting the shop.\n");
return 0;
}
But my point is that you have mistakes that have nothing to do with linked list insertion per se. Basically, you allocated space for the nodes, but you failed to allocate space for the strings. Take a look:
Code:
char *input = malloc(25);
printf("Enter data separated by tabs.\n");
/*Get the user input then parse it into the separate data pieces.*/
fgets(input, 25, stdin);
sscanf(input, "%s\t%s\t%d\t%lf", current->name, current->shelf, ¤t->stock, ¤t->price);
free(input);
In the sscanf call, you assume that space has been allocated for current->name and current->shelf, but now with proper formatting, a quick check of main shows that you never did allocate space for them. Instead, you did unnecessary complication with the malloc(25) call (and likewise for command) when you could have written:
Code:
char input[25] = "";
Furthermore, you don't specify a field with, i.e., you use "%s" instead of say "%15s", so your sscanf call is vulnerable to buffer overflow.
Hence, you should work with int objects instead of a struct object value for your linked list node first, and later come back to deal with nodes containing struct objects.