My func looks like:
int pop(node **hd, char **new_element) { node *dummy; int i = 0; if (empty(*hd) == 1) { printf("Stack is empty"); return -1; } else { (*new_element)[i] = (*hd)->element[i]; // problem is here ;) dummy = *hd; *hd = (*hd)->next; free(dummy); }and I call it via:
2. char *x = malloc(sizeof(char)*MAX_LINE_LNGTH + 1); x = "foo"; pop(&top, &x);It crashes every time I assign some value to the (*new_element)[i]. I can read it's value by and print it, but not change it.
I'm a C beginner so I appreciate any help. Thanks in advance.
The basic thing what you are trying to do is,
1. Allocate a memory to a pointer x [ suppose x points now to 1000]
2. x = "foo" , This will change the pointer x to point to address which holds the
string "foo" [say, to 3000]
This assignment makes step1 useless.
Since "foo" is a string constant, the memory which contains "foo" is in
the read only area; which means, you cannot write to the memory pointed by x.
3. When you try to modify the read only area pointed to by x, you get a crash because of the reason stated in 2.