G
grishin-mailing-lists
Hello,
Now I'm reading Unleashed C, a book written by the community.
I like the style it's written. There are a lot of fun, I appreciate
it. Thank you guys.
There is a point I can't get for the last 2 days, though Ben explained
it. Possible the hassle is due to a poor translation from English into
Russian (Unfortunately I'm reading a translation).
Here we have a double pointer "new". I can't get what is it for and
the way it's used.
/* bin.c lines 79-109 */
int bin_insert(struct bin_tree *tree, int item)
{
struct bin_node *node, **new;
assert(tree != NULL);
new = &tree->root;
node = tree->root;
for (; {
if (node == NULL) {
node = malloc(sizeof *node);
*new = node;
if (node != NULL) {
node->data = item;
node->left = node->right = NULL;
tree->count++;
return 1;
}
else
return 0;
}
else if (item == node->data)
return 2;
else if (item > node->data) {
new = &node->right;
node = node->right;
}
else {
new = &node->left;
node = node->left;
}
}
}
On one hand the value stored in the variable has never been used. On
the other hand I tried to comment all "new" entries and eventually the
program doesn't work properly printing
D:\projects\MinGW\ch12\wpj>noname.exe
Seed value = 23488
Inserted 11:
Tree has 0 nodes, but tree count is 1.
Error(s) encountered, aborting execution.
So it used in a mysterious way (as for me).
In addition the same technique is used afterwards so I can't sluff it
(otherwise I would be sensible to skip the chapter altogether).
Thank you in advance.
Now I'm reading Unleashed C, a book written by the community.
I like the style it's written. There are a lot of fun, I appreciate
it. Thank you guys.
There is a point I can't get for the last 2 days, though Ben explained
it. Possible the hassle is due to a poor translation from English into
Russian (Unfortunately I'm reading a translation).
Here we have a double pointer "new". I can't get what is it for and
the way it's used.
/* bin.c lines 79-109 */
int bin_insert(struct bin_tree *tree, int item)
{
struct bin_node *node, **new;
assert(tree != NULL);
new = &tree->root;
node = tree->root;
for (; {
if (node == NULL) {
node = malloc(sizeof *node);
*new = node;
if (node != NULL) {
node->data = item;
node->left = node->right = NULL;
tree->count++;
return 1;
}
else
return 0;
}
else if (item == node->data)
return 2;
else if (item > node->data) {
new = &node->right;
node = node->right;
}
else {
new = &node->left;
node = node->left;
}
}
}
On one hand the value stored in the variable has never been used. On
the other hand I tried to comment all "new" entries and eventually the
program doesn't work properly printing
D:\projects\MinGW\ch12\wpj>noname.exe
Seed value = 23488
Inserted 11:
Tree has 0 nodes, but tree count is 1.
Error(s) encountered, aborting execution.
So it used in a mysterious way (as for me).
In addition the same technique is used afterwards so I can't sluff it
(otherwise I would be sensible to skip the chapter altogether).
Thank you in advance.