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:
void print(const struct Line* n)
{
for ( ; n; n = n->next )
printf("%s\n", n->tekst);
printf("\n");
}
Putting it together in 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. 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. 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. Just like we stored the address of an int in b, notice how we store the address of a int* in 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. 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.
nextparameter toadd(), thereby invoking undefined behavior when you evaluate that during yourprint()enumeration. Just in case you missed that. I.e.tekstinmain()is uninitialized.nextvalue while calling myadd()function. That's one of things I'm asking for to get help with ;)tekstto NULL before sending it as thenextvalue toadd(). Honestly, however, this is rather odd for linked list insertion code to begin with. Normally the insertion takes a pointer-to-pointer for the list head, and there are hundreds of examples of this on SO.add()function is developed.