问题
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);
}
回答1:
Your create
function is incorrect:
- you do not test for potential
malloc
failure - you do not copy the string, but merely cause undefined behavior by attempting to write to
clothing[10]
which is beyond the end of the array. By the way, you readch[10]
which may well be out of bounds too. You should instead copy the string, while avoiding a buffer overflow ifch
is too long.
Here is an improved version:
#incude <string.h>
#incude <stdlib.h>
node *create(char const ch[]) {
temp = malloc(sizeof(node));
if (temp != NULL) {
temp->next = NULL;
temp->clothing[0] = '\0';
strncat(temp->clothing, ch, sizeof(temp->clothing) - 1);
}
return temp;
}
Since C99, there is a way to allocate a copy of the string without a limitation on its size, and without the need for a separate allocation and a pointer in the node
structure. It is called a flexible array. Here is how it works:
typedef struct node {
struct node *next;
char clothing[];
} node;
node *create(char const ch[]) {
size_t size = strlen(ch) + 1;
temp = malloc(sizeof(node) + size);
if (temp != NULL) {
temp->next = NULL;
memcpy(temp->clothing, ch, size);
}
return temp;
}
回答2:
node *addnode(node *after, const char *str)
{
node *nptr;
nptr = malloc(sizeof(*nptr));
nptr -> partstr = malloc(strlen(str) + 1);
/* error checking you need to add after every malloc */
strcpy(nptr -> partstr, str);
if(!after)
{
nptr -> prev = NULL;
nptr -> next = NULL;
}
else
{
after -> next -> prev = nptr;
nptr -> next = after -> next;
after -> next = nptr;
nptr -> prev = after;
}
return nptr;
}
回答3:
I have pass the node to print function to print the string but nothing is getting printed.
doing
temp -> clothing[10] = ch[10];
you write (and may be read) one character out of the string, the max index in temp -> clothing
is 9
you want something like
strcpy(temp -> clothing, ch);
but take care to not go outside the field clothing because ch is too long
so can be
strncpy(temp -> clothing, ch, sizeof(temp -> clothing) - 1);
temp -> clothing[sizeof(temp -> clothing) - 1] = 0; /* useful if strlen(ch) >= 10 */
are you sure you do not want to replace char clothing[10];
by char * clothing;
to not have that limit to 10 ?
来源:https://stackoverflow.com/questions/55550666/creating-linked-list-of-strings