12 Dec, 2009, JohnnyStarr wrote in the 1st comment:
Votes: 0
This may be a compiler specific question, but I'm curious why this works:

if (ch)


But, this does not:
for ( ch = char_list; ch; ch = ch->next )


Diku always uses ch != NULL, which makes sense, but shouldn't ch being TRUE have the
same effect? Does it have to do with the fact that ch->next points to the next node that
is in a sense TRUE, even though it is NULL?
12 Dec, 2009, Davion wrote in the 2nd comment:
Votes: 0
They do have the same effect…
12 Dec, 2009, Twisol wrote in the 3rd comment:
Votes: 0
From what I know and have seen, they should both work properly.
12 Dec, 2009, David Haley wrote in the 4th comment:
Votes: 0
You'd have to define what you mean by the latter not working. It will certainly do something reasonable; the question is if it's doing what you want it to do…
12 Dec, 2009, Tyche wrote in the 5th comment:
Votes: 0
Yes it works…

Demonstration testfor.c
#include <stdio.h>
struct foo {int x;struct foo * next;};
struct foo * list = NULL;

l_add(int i) {
struct foo* f = (struct foo*) malloc(sizeof(*f));
f->x = i;
if (!list)
f->next = NULL;
else
f->next = list;
list = f;
}

int main() {
int i;
struct foo* f;
for (i=1;i<10;i++) l_add(i);
for (f=list;f;f=f->next)
printf("%d\n",f->x);
/* too lazy to free data */
}


$ gcc testfor.c ; ./a
9
8
7
6
5
4
3
2
1
12 Dec, 2009, JohnnyStarr wrote in the 6th comment:
Votes: 0
Well this was a waste of space :redface:
I'll have to figure out why my codes not working.
12 Dec, 2009, Zen_Clark wrote in the 7th comment:
Votes: 0
JohnnyStarr said:
Well this was a waste of space :redface:
I'll have to figure out why my codes not working.


Is the force strong enough within you? You may just need to be more sensitive to the ways of the force.
12 Dec, 2009, JohnnyStarr wrote in the 8th comment:
Votes: 0
Zen_Clark said:
JohnnyStarr said:
Well this was a waste of space :redface:
I'll have to figure out why my codes not working.

Is the force strong enough within you? You may just need to be more sensitive to the ways of the force.

Right…
13 Dec, 2009, quixadhal wrote in the 9th comment:
Votes: 0
Do you modify the list anywhere inside your loop body?
13 Dec, 2009, Skol wrote in the 10th comment:
Votes: 0
One thing I do (may or may not be redundant) is do two, so I'm not automatically trying to point to a NULL.

Like:
CHAR_DATA *ch, *ch_next;

for (ch = char_list; ch; ch = ch_next)
{
ch_next = ch->next;

// blah blah blah, etc
}
13 Dec, 2009, Davion wrote in the 11th comment:
Votes: 0
Make sure when the node is constructed, ->next is initialized to NULL, or ch wont ever end up being NULL.
13 Dec, 2009, JohnnyStarr wrote in the 12th comment:
Votes: 0
Thanks, it was something little. I guess I overlooked it awhile back. :lol:
0.0/12