[英]Linked string list C / Struct
I'm to create a LinkedList that has functionalities of adding and printing strings within. 我要创建一个具有在其中添加和打印字符串的功能的LinkedList。
Attaching my code below. 在下面附加我的代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Line {
char tekst[20];
struct Line *next;
};
void print(const struct Line* n);
void add(struct Line* n, char t[20], struct Line* next);
int main(void) {
struct Line lin, *tekst;
tekst = NULL;
add(&lin, "ExampleString1", tekst);
add(&lin, "ExampleString2", tekst);
print(&lin);
getch();
return 0;
};
void print(const struct Line* n) {
for ( ; n; n = n->next )
printf("%s", n->tekst);
printf("\n");
}
void add(struct Line* n, char t[20], struct Line* next) {
strcpy(n->tekst,t); // before: n->tekst[20] = t[20];
n->next = next;
}
It writes some random numbers on the standard output, and then crashes the commandline. 它将一些随机数写入标准输出,然后使命令行崩溃。 I've no idea whether
tekst2[20]
should even be here (I'm not sure how to call my function arguments here). 我不知道
tekst2[20]
是否应该在这里(我不确定如何在这里调用函数参数)。
My goal is to make a list of strings then be able to add and print them. 我的目标是制作一个字符串列表,然后可以添加和打印它们。
I'm nearly certain this is what you're trying to do: 我几乎可以确定这是您要执行的操作:
void add(struct Line** pp, const char *t)
{
struct Line *p = malloc(sizeof(*p));
strcpy(p->tekst, t);
p->tekst2[0] = 0;
p->next = NULL;
while (*pp)
pp = &(*pp)->next;
*pp = p;
}
Your print()
function is using the wrong format specifier for printing a string: 您的
print()
函数使用错误的格式说明符来打印字符串:
void print(const struct Line* n)
{
for ( ; n; n = n->next )
printf("%s\n", n->tekst);
printf("\n");
}
Putting it together in main()
: 将其放到
main()
:
int main(void)
{
struct Line *lst = NULL;
add(&lst, "SomeTxt");
add(&lst, "SomeMoreTxt");
add(&lst, "YetMoreTxt");
print(lst);
getch();
return 0;
};
Output 产量
SomeTxt
SomeMoreTxt
YetMoreTxt
I leave the proper list cleanup code as well as proper error checking as a task for you. 我将适当的列表清除代码以及正确的错误检查作为任务留给您。
How It Works 这个怎么运作
This function utilizes a "pointer-to-pointer" idiom. 此功能使用“指针到指针”的成语。 When you pass
&lst
from main()
, you're passing the address of a pointer variable. 从
main()
传递&lst
,您正在传递指针变量的地址 。 Pointers are nothing more than variables that hold addresses to stuff (of the type the pointer it declared, of course). 指针不过是保存东西地址的变量(当然,它是声明的指针的类型)。 Example:
例:
int a;
int *b = &a;
declares a pointer-to- int
, and assigns the address of an int
to store within it. 声明一个指向
int
的指针,并分配要存储在其中的int
的地址 。 On the other hand, this: 另一方面,这是:
int a;
int *b = &a;
int **c = &b;
declares what we had before, but c
is declared to be a pointer-to-pointer-to-int . 声明我们以前拥有的东西,但是
c
声明为pointer-to-pointer-to-int 。 Just like we stored the address of an int
in b
, notice how we store the address of a int*
in c
. 就像我们将
int
的地址存储在b
,请注意我们如何将int*
的地址存储在c
。 this is a very powerful feature of the language. 这是该语言非常强大的功能。
That said, the code works like this: 也就是说,代码的工作方式如下:
void add(struct Line** pp, const char *t)
{
// node allocation stuff. nothing special here
struct Line *p = malloc(sizeof(*p));
strcpy(p->tekst, t);
p->tekst2[0] = 0;
p->next = NULL;
// look at the pointer addressed by our pointer-to-pointer pp
// while it is not null, store the address of the `next` pointer
// of that node in pp and loop.
while (*pp)
pp = &(*pp)->next;
// pp now holds the address of the pointer we want to set with our new node
// it could be the address of the original pointer passed in (if the list was
// empty). or the address of some `next` member in the list. We really don't
// care which. All we care about it is it addresses the pointer we need to
// assign our new allocation to, so that is what we do.
*pp = p;
}
Spend some time on the net researching C and pointers to pointers. 花一些时间在网上研究C和指向指针的指针。 They will change the way you think about things like list management.
它们将改变您对列表管理之类的想法。 The most important thing to remember is a pointer to pointer does not pointer to some "node";
要记住的最重要的事情是一个指针,指针不指向某个“节点”; it points to a pointer that points to a node.
它指向一个指向节点的指针 。 That is a pretty heady statement, but do some homework and it will make sense.
这是一个令人头疼的声明,但要做一些功课就可以了。
Edited:Use line below (or strncpy
) strcpy(n->tekst,t);
编辑:使用下面的行(或
strncpy
) strcpy(n->tekst,t);
instead of n->tekst[20] = t[20];
而不是
n->tekst[20] = t[20];
Initialize *test to null 初始化* test为null
struct Line lin, *tekst=NULL;
Use %s to print text in your program. 使用%s在程序中打印文本。
printf("%s", n->tekst);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.