-3

is there any problem with this code . i can't decode huffman with it. fault : segmentation fault (core dumped)

char *real_bin = "100101111101001100"

struct tree
{
  int fre;
  char c;
  struct tree *left;
  struct tree *right;
};
 typedef struct tree tree;


void decode_huffman(tree *root, char *real_bin)
{

  tree *p = root;

  int m = strlen(real_bin);

  for (int i = 0; i < m; i++)
  {
    if (real_bin[i] == '0')
      {
        p = p->left;
      }

    else
      {
        p = p->right;
      }

    if (p->c != '\0')
      {
        printf("%c\n", p->c);
        p = root;
      }
    }
  }   
  • 2
    Yes. This code is incomplete. Please post [mcve]. – Eugene Sh. Jan 22 '19 at 18:01
  • if you get segfault and it's not obvious why, it's time to use a debugger to find out which line the segfault is happening. – erik258 Jan 22 '19 at 18:02
  • help me ! please... – Son Lam Thai Jan 22 '19 at 18:02
  • People cannot help you if you do not provide sufficient information. Either a [Minimum, Complete, Verifiable Example](https://stackoverflow.com/help/mcve), or at least some extra information to where the problem happens. Just run your program with a debugger and you will know where the problem happens. – Alain Merigot Jan 22 '19 at 18:13
  • i provided them in my answer. can you help me ? – Son Lam Thai Jan 22 '19 at 18:19
  • I used a debugger but no result :( – Son Lam Thai Jan 22 '19 at 19:10
  • It is not possible to explain what is wrong with this code, because we cannot see the all-important `main` function. That is the code that is responsible for creating a tree, assuming of course that the problem isn't simply _you forgot to create a tree_, and also for passing the parameters to the `decode_huffman` function – Tim Randall Jan 22 '19 at 19:23
  • I pretty sure about my code for creating a tree is excellent . – Son Lam Thai Jan 22 '19 at 19:51

1 Answers1

0

Well if you are so sure about the coding and you tree then you should adjust your code this way

void decode_huffman(tree *root, char *real_bin)
{

  tree *p = root;

  int m = strlen(real_bin);

  for (int i = 0; i < m; i++)
  {
    if (real_bin[i] == '0')
      {
        p = p->left;
      }

    else 
      {
        p = p->right;
      }

      if (p->left==NULL and p->right==NULL) 
    { 
        printf("%c\n",p->data); 
        p=root;

    } 
    }
  }   
Spinkoo
  • 2,080
  • 1
  • 7
  • 23