函数中的双指针

问题描述 投票:0回答:1

我试图在二叉搜索树中插入字符串。

所以我要尝试的是,

解析文件中的字符串(包含指令集),然后插入函数中

insertOpcodeFromFile()。

所以这个函数会执行

(* node)= Node_insert(&node,instruction)。

节点将是位于main函数中的二叉树的根。

所以用简单的方式来解释一下,我想通过在其他函数中使用双指针来操作(插入)主函数中的根指针包含插入函数。

我对指针有一个简单的了解,但在这种情况下,我需要使用多个双指针。

请用这个例子清楚地解释一下双指针。

这是我的代码(我注释掉了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;
}
c pointers
1个回答
0
投票

在这种情况下,在main()

Node *root;

为什么需要在改变Node **的函数中使用“双”指针(root)是因为root值要在这些函数中设置。

例如,假设您要分配Node并将其设置为root。 如果您执行以下操作

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

使用指针指针(你称之为“双指针”)

void alloc_root(Node **root) {
   *root = malloc(sizeof (Node)); // note the *
}
...
   // then in main
   allow_root( &root );
   // here main's root is set

混乱可能来自主要的Node *rootroot是指向Node的指针。你如何在函数int i;中设置整数f?您可以使用f(&i)调用函数f(int *p) { *p = 31415; }i设置为值31415

root视为包含Node地址的变量,并在函数中设置其值,您必须通过&rootroot是一个Node *,它使另一个*,像func(Node **p)

© www.soinside.com 2019 - 2024. All rights reserved.