[英]Double pointer in the function
I am trying to insert strings in the binary search tree. 我正在尝试在二进制搜索树中插入字符串。
So what I am to trying is, 所以我要尝试的是
parsing strings from a file(contains instruction set) and then inserting in the function 解析文件中的字符串(包含指令集),然后插入函数中
insertOpcodeFromFile(). insertOpcodeFromFile()。
So this function will execute 所以这个功能会执行
(*node) = Node_insert(&node,instruction). (* node)= Node_insert(&node,指令)。
the node will be the root of binary tree which is located in main function. 该节点将是位于主函数中的二叉树的根。
So in simple way to explain, I want to manipulate(insert) the root pointer in the main function by using double pointer in the other function contain insert function. 因此,以简单的方式进行说明,我想通过在其他函数包含插入函数中使用双指针来操纵(插入)主函数中的根指针。
I have a simple understanding about the pointer, but in this situation, I need to use more than double pointer I think. 我对指针有一个简单的了解,但是在这种情况下,我需要使用多个双指针。
please explain me about the double pointer clearly using this example. 请使用此示例清楚地向我解释有关双指针的内容。
Here is my code(I commenting out insert_node) 这是我的代码(我注释掉insert_node)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
typedef struct node *NodePtr;
typedef struct node {
char *word;
int count;
NodePtr left;
NodePtr right;
} Node;
NodePtr Node_alloc();
NodePtr Node_insert(NodePtr node_ptr, char *word);
void clearArray(char a[]);
void insertOpcodeFromFile(FILE *opcodeFile, NodePtr *node);
void Node_display(NodePtr);
char *char_copy(char *word);
#endif
int main(int argc, const char * argv[]) {
FILE * opFile;
FILE * progFile;
struct node *root = NULL;
if ( argc != 4) { // # of flag check
fprintf(stderr, " # of arguments must be 4.\n" );
exit(1);
}
opFile = fopen ( argv[1], "r");
if(opFile == NULL)
{
fprintf(stderr,"There is no name of the opcode file\n");
exit(1);
}
progFile = fopen ( argv[2], "r");
if(progFile == NULL)
{
fprintf(stderr,"There is no name of the program file \n");
exit(1);
}
insertOpcodeFromFile(opFile, &root);
//Node_display(root);
}/* main is over */
void insertOpcodeFromFile(FILE *opcodeFile, NodePtr *node)
{
int fsize = 0;
int lengthOfInst = 0;
int c;
int i;
char buffer[100];
fsize = getFileSize(opcodeFile);
enum flag {ins,opc,form};
int flag = ins;
char instruction[6];
unsigned int opcode = 0;
unsigned char format;
while (c != EOF)
{
c = fgetc(opcodeFile);
buffer[i++] = c;
if (c == 32){
switch (flag) {
case ins:
flag = opc;
memcpy(instruction,buffer,i);
instruction[i] = '\0';
clearArray(buffer);
i = 0;
// printf("인스트럭션 : %s\n",instruction );
break;
case opc:
flag = form;
opcode = atoi(buffer);
clearArray(buffer);
i = 0;
// printf("옵코드 : %d\n",opcode );
break;
default:
break;
}/* end of switch */
}/* end of if(space) */
if((c == 10) || (c == EOF))
{
if (flag == form)
{
format = buffer[0];
clearArray(buffer);
i = 0;
// printf("포멧: %c\n", format);
}
flag = ins;
//node = Node_insert(node,instruction);
}
}
//Node_display(node);
}
int getFileSize(FILE *opcodeFile)
{ int fsize = 0;
fseek(opcodeFile,0, SEEK_SET);
fseek(opcodeFile,0, SEEK_END);
fsize = (int)ftell(opcodeFile);
fseek(opcodeFile,0, SEEK_SET);
return fsize;
}
int countUntilSpace(FILE *opcodeFile, int currentPosition)
{ char readword[1];
char *space = " ";
char *nextLine = "/n";
int i = 0;
//printf("현재: %d\n",currentPosition );
while(1)
{
fread(readword, sizeof(char),1,opcodeFile);
i++;
if(strcmp(readword,space) == 0 || strcmp(readword,nextLine) == 0)
{
//printf("break\n");
break;
}
}
fseek(opcodeFile,currentPosition ,SEEK_SET);
//printf("끝난 현재 :%d\n",ftell(opcodeFile) );
//printf("%I : %d\n",i );
return i - 1;
}
void clearArray(char a[])
{
memset(&a[0], 0, 100);
}
NodePtr Node_alloc()
{
return (NodePtr) malloc(sizeof(NodePtr));
}
NodePtr Node_insert(NodePtr node_ptr, char *word)
{
int cond;
if (node_ptr == NULL) {
node_ptr = Node_alloc();
node_ptr->word = char_copy(word);
node_ptr->count = 1;
node_ptr->left = node_ptr->right = NULL;
} else if ((cond = strcmp(word, node_ptr->word)) == 0) {
node_ptr->count++;
} else if (cond < 0) {
node_ptr->left = Node_insert(node_ptr->left, word);
} else {
node_ptr->right = Node_insert(node_ptr->right, word);
}
return node_ptr;
}
void Node_display(NodePtr node_ptr)
{
if (node_ptr != NULL) {
Node_display(node_ptr->left);
printf("%04d: %s\n", node_ptr->count, node_ptr->word);
Node_display(node_ptr->right);
}
}
char *char_copy(char *word)
{
char *char_ptr;
char_ptr = (char *) malloc(strlen(word) + 1);
if (char_ptr != NULL) {
char_ptr = strdup(word);
}
return char_ptr;
}
In this case, in main()
, 在这种情况下,在
main()
,
Node *root;
Why do you need to use a "double" pointer ( Node **
) in functions that alter root
is because root
value as to be set in these functions. 为什么在更改
root
函数中需要使用“双”指针( Node **
),是因为要在这些函数中设置root
值。
For instance, say you want to allocate a Node
and set it into root
. 例如,假设您要分配一个
Node
并将其设置为root
。
If you do the following 如果您执行以下操作
void alloc_root(Node *root) {
root = malloc(sizeof (Node));
// root is a function parameter and has nothing to do
// with the 'main' root
}
...
// then in main
alloc_root( root );
// here main's root is not set
Using a pointer to pointer (that you call "double pointer") 使用指向指针的指针(称为“双指针”)
void alloc_root(Node **root) {
*root = malloc(sizeof (Node)); // note the *
}
...
// then in main
allow_root( &root );
// here main's root is set
The confusion comes probably from the Node *root
in main, root
being a pointer to a Node
. 混乱可能来自于main中的
Node *root
, root
是指向Node
的指针。 How would you set an integer int i;
你将如何设置一个整数
int i;
in a function f
? 在函数
f
? You would use f(&i)
to call the function f(int *p) { *p = 31415; }
您可以使用
f(&i)
调用函数f(int *p) { *p = 31415; }
f(int *p) { *p = 31415; }
to set i
to the value 31415
. f(int *p) { *p = 31415; }
将i
设置为值31415
。
Consider root
to be a variable that contains an address to a Node
, and to set its value in a function you have to pass &root
. 将
root
视为包含Node
的地址的变量,并在必须通过&root
传递的函数中设置其值。 root
being a Node *
, that makes another *
, like func(Node **p)
. root
是一个Node *
,它形成另一个*
,例如func(Node **p)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.