C 指针的结构和引用的指针
C Pointer of Pointer of struct and reference
我正在尝试用 C 实现 BST。代码如下:
int main(int argc, char* argv[]) {
int_bst_node_t * tree_p = NULL;
test_insert(&tree_p, 40);
}
static void test_insert(int_bst_node_t ** t_p, int n) {
printf("inserting %d into tree ", n);
printf("\n");
if (int_bst_insert(t_p, n)) {
printf("result ");
print_tree_elements(*t_p);
printf("\n");
}
else {
printf("insert failed\n");
}
}
其他文件中的代码:
// Create a node, assign values
bool createNode(int n, int_bst_node_t *node) {
int_bst_node_t *localNode = (struct int_bst_node*)malloc(sizeof(int_bst_node_t));
if( localNode == NULL )
{
puts("\n Unable to allocate memory");
return false;
}
localNode->data = n;
localNode->left = NULL;
localNode->right = NULL;
node = localNode;
////// Prints right values
printf("\n LocalNode Data: %d Node Data: %d", localNode->data, node->data);
free(localNode);
return true;
}
/*
* insert n into *t_p
* does nothing if n is already in the tree
* returns true if insertion succeeded (including the case where n is already
* in the tree), false otherwise (e.g., malloc error)
*/
bool int_bst_insert(int_bst_node_t ** t_p, int n) {
if (*t_p == NULL) {
printf("*t_p %d IS null", *t_p);
int_bst_node_t node;
// Pass node as a ref, so data is updated
if (createNode(n, &node) == false)
return false;
// Prints invalid data
printf("\n Data of new node : %d", node.data);
*t_p = &node;
/*
Need to assign node to *t_p, so it is updated, and prints the values
when calling next function in test_insert()
int_bst_node_t *t = *t_p;
printf("\n Data of *tp node : %d", t->data);
*/
} else
printf("*t_p %d is NOT null", *t_p);
printf("\n");
return true;
}
我无法将节点的 value/ref 设置为 t_p。这仅适用于根节点;进一步它将是更多的节点。我无法获得 ** 的概念来更新值。我尝试了变体,都失败了。
如果有人能帮助我,我会很高兴。
谢谢。
函数createNode
没有意义至少因为它按值接受指向节点的指针。在为节点分配内存后,您立即释放它,使指向分配内存的指针无效。
int_bst_node_t *localNode = (struct int_bst_node*)malloc(sizeof(int_bst_node_t));
//...
node = localNode;
//...
free(localNode);
函数int_bst_insert
同样没有意义。除了像这样的错误
*t_p = &node;
它应该插入一个相对于二叉搜索树中已经存在的节点的值的节点。
注意函数不输出任何信息
可以通过以下方式定义函数。
int_bst_node_t * createNode( int n )
{
int_bst_node_t *localNode = malloc(sizeof(int_bst_node_t));
if( localNode != NULL )
{
localNode->data = n;
localNode->left = NULL;
localNode->right = NULL;
}
return localNode;
}
和
bool int_bst_insert( int_bst_node_t ** t_p, int n )
{
while ( *t_p != NULL )
{
if ( n < ( *t_p )->data )
{
t_p = &( *t_p )->left;
}
else
{
t_p = &( *t_p )->right;
}
}
*t_p = createNode( n );
return *t_p != NULL;
}
我正在尝试用 C 实现 BST。代码如下:
int main(int argc, char* argv[]) {
int_bst_node_t * tree_p = NULL;
test_insert(&tree_p, 40);
}
static void test_insert(int_bst_node_t ** t_p, int n) {
printf("inserting %d into tree ", n);
printf("\n");
if (int_bst_insert(t_p, n)) {
printf("result ");
print_tree_elements(*t_p);
printf("\n");
}
else {
printf("insert failed\n");
}
}
其他文件中的代码:
// Create a node, assign values
bool createNode(int n, int_bst_node_t *node) {
int_bst_node_t *localNode = (struct int_bst_node*)malloc(sizeof(int_bst_node_t));
if( localNode == NULL )
{
puts("\n Unable to allocate memory");
return false;
}
localNode->data = n;
localNode->left = NULL;
localNode->right = NULL;
node = localNode;
////// Prints right values
printf("\n LocalNode Data: %d Node Data: %d", localNode->data, node->data);
free(localNode);
return true;
}
/*
* insert n into *t_p
* does nothing if n is already in the tree
* returns true if insertion succeeded (including the case where n is already
* in the tree), false otherwise (e.g., malloc error)
*/
bool int_bst_insert(int_bst_node_t ** t_p, int n) {
if (*t_p == NULL) {
printf("*t_p %d IS null", *t_p);
int_bst_node_t node;
// Pass node as a ref, so data is updated
if (createNode(n, &node) == false)
return false;
// Prints invalid data
printf("\n Data of new node : %d", node.data);
*t_p = &node;
/*
Need to assign node to *t_p, so it is updated, and prints the values
when calling next function in test_insert()
int_bst_node_t *t = *t_p;
printf("\n Data of *tp node : %d", t->data);
*/
} else
printf("*t_p %d is NOT null", *t_p);
printf("\n");
return true;
}
我无法将节点的 value/ref 设置为 t_p。这仅适用于根节点;进一步它将是更多的节点。我无法获得 ** 的概念来更新值。我尝试了变体,都失败了。
如果有人能帮助我,我会很高兴。
谢谢。
函数createNode
没有意义至少因为它按值接受指向节点的指针。在为节点分配内存后,您立即释放它,使指向分配内存的指针无效。
int_bst_node_t *localNode = (struct int_bst_node*)malloc(sizeof(int_bst_node_t));
//...
node = localNode;
//...
free(localNode);
函数int_bst_insert
同样没有意义。除了像这样的错误
*t_p = &node;
它应该插入一个相对于二叉搜索树中已经存在的节点的值的节点。
注意函数不输出任何信息
可以通过以下方式定义函数。
int_bst_node_t * createNode( int n )
{
int_bst_node_t *localNode = malloc(sizeof(int_bst_node_t));
if( localNode != NULL )
{
localNode->data = n;
localNode->left = NULL;
localNode->right = NULL;
}
return localNode;
}
和
bool int_bst_insert( int_bst_node_t ** t_p, int n )
{
while ( *t_p != NULL )
{
if ( n < ( *t_p )->data )
{
t_p = &( *t_p )->left;
}
else
{
t_p = &( *t_p )->right;
}
}
*t_p = createNode( n );
return *t_p != NULL;
}