Originally Posted by
Salem
You need to show us your StrStackPush() and how you call it.
Because if you don't know where your strings are, chances are - you're doing it wrong.
So, in this particular example:
Code:
void StrStackPush(StrStack* stack, char* s)
{
assert( stack != NULL );
StrStackLink *p = (StrStackLink*)
malloc(sizeof(StrStackLink));
if (p == NULL)
{
printf("out of memory, cannot push a string to the
stack\n");
return;
}
p->str = s;
p->next = stack->top;
stack->top = p;
}
Code:
#include <stdlib.h>
#include <stdio.h>
#include "StrStack.h"
char * readline() { ... } //A function to read a line
int main() {
char *line;
StrStack *stack = StrStackNew();
while ((line = readline()) != NULL)
{
StrStackPush(stack, line);
}
while ((line = StrStackPop(stack)) != NULL)
{
printf("%s\n", line);
free(line);
}
StrStackFree(&stack);
return 0;
}
Where readline function returns a char pointer.
So as john has described - our str's here live in the main stack frame, which is also in charge of creating new pointers to new chars (by readline) using the temporary line pointer.
So, the pointer of each str lives as long as the main lives - which is until the end of the program.