I am new to data structures as well as to linked lists. I am working on project named as Amazon product availability checker using tree in C. So I want to store strings in each node of the tree but while storing strings the code is not showing any error but the output is also not getting printed. I have pass the node to print function to print the string but nothing is getting printed.
I have shared the code for one string and one node only. I am working on ubuntu and I am coding in the C language.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char clothing[10];
struct node *next;
} node;
// 1) creating node
node *create(char const ch[]) {
int i;
node *head = NULL;
node *temp = NULL;
node *p = NULL;
temp = (struct node *)malloc(sizeof(struct node));
temp->clothing[10] = ch[10];
temp->next = NULL;
return temp;
}
// 2) print
void print(node *head) {
node *p = head;
while (p != NULL) {
printf("%s", p->clothing);
p = p->next;
}
}
int main() {
node *head = create("clothing");
print(head);
}
create,tempandpare unused, so delete them. And usestrcpyto copy the string.temp -> clothing[10] = ch[10]is assigning a single character, and in fact that character is out of bounds so you are corrupting memory.