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:

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 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.

don't pass that char* as a double pointer when all it needs is a single pointer int pop(node **hd, char *new_element) { Then it can easily be acessed new_element[i] = (*hd)->element[i]; If the above doesn't fix the problem then it is in how it's accessing hd pointer.


>>char *x = malloc(sizeof(char)*MAX_LINE_LNGTH + 1);
The use of sizeof(char) here is superfluous. sizeof(char) is guarenteed to always be 1.

Edited 6 Years Ago by Ancient Dragon: n/a

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 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.

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.

Ah, now I get the reason of the value changing error. Do you have any solution to my problem?

Ah, now I get the reason of the value changing error. Do you have any solution to my problem?

The solution is to avoid that assignment [x="foo"] as I think it is useless and follow the method of passing a single pointer to that function as done by AncientDragon.
You need to pass double pointer [&x]only if you want to change the pointer 'x' not its value.To change the value, you just need to pass a single pointer[ie,x]

I'm not sure but i think that you should try *x = "foo"; instead of x = "foo"; because it is a pointer to a string (actually a series of chars) and its values are not chars themselves. they are addresses.

I repeat, I'm not sure if right. I'm just suggesting

The solution is to avoid that assignment [x="foo"] as I think it is useless and follow the method of passing a single pointer to that function as done by AncientDragon.
You need to pass double pointer [&x]only if you want to change the pointer 'x' not its value.To change the value, you just need to pass a single pointer[ie,x]

Hm, how should I avoid it? I need to pass there some String, because I need a String LIFO. Thank you for your replies.

I'm not sure but i think that you should try *x = "foo"; instead of x = "foo"; because it is a pointer to a string (actually a series of chars) and its values are not chars themselves. they are addresses.

I repeat, I'm not sure if right. I'm just suggesting

assignment makes integer from pointer without a cast

:/

Hm, how should I avoid it? I need to pass there some String, because I need a String LIFO. Thank you for your replies.

I will use

char *x = malloc(sizeof(char) * 10);
                memset(x,'\0',10);
                strcpy(x,"foo");

to get the string "foo" into x.

char *x = malloc(sizeof(char) * 10);
                memset(x,'\0',10);
                strcpy(x,"foo");

Nice! Thank you a lot! I forgotthat C has this mem moving functions, as I said I'm C beginner.

Thank you once more time, sir! :)

This question has already been answered. Start a new discussion instead.