Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 659217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:01:12+00:00 2026-05-13T23:01:12+00:00

i declare a global variable and use and modify its value in the function.

  • 0

i declare a global variable and use and modify its value in the function. Then i want to get the modified value of this global variable, it has some problem. Can anyone help me?

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct link
{
            int freq;
            char value[MAX];
            struct link* right;
            struct link* left;
};
typedef struct link node;
void sort(node *[], int);
node* create(char[], int);
void sright(node *[], int);
void Assign_Code(node*, int [], int);
void Delete_Tree(node *);


int test[720][720];

main()
{
    node* ptr, * head;
    int i, n, total = 0, u, c[256];
    char str[MAX];
    node* a[256];
    int freq;

    printf(  "Huffman Algorithm\n");
    printf("\nEnter the no. of letter to be coded:");
    /*input the no. of letters*/
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        printf("Enter the letter & frequency:");
        /*input the letter & frequency*/
        scanf("%s %d", str, &freq);
        a[i] = create(str, freq);
    }
    while (n > 1)
    {
        sort(a, n);
        u = a[0]->freq + a[1]->freq;
        strcpy(str,a[0]->value);
        strcat(str,a[1]->value);
        ptr = create(str, u);
        ptr->right = a[1];
        ptr->left = a[0];
        a[0] = ptr;
        sright(a, n);
        n--;
    }

    Assign_Code(a[0], c, 0);
   //getch();
    printf("Code: ");
      for (i = 1; i <= n; i++)
        {
            printf("%d", test[0][i]);
        }
        printf("\n");

    Delete_Tree(a[0]);


}

node* create(char a[], int x)
{
    node* ptr;
    ptr = (node *) malloc(256*sizeof(node));
    ptr->freq = x;
    strcpy( ptr->value , a);
    ptr->right = ptr->left = NULL;
    return(ptr);
}
void sort(node* a[], int n)
{
    int i, j;
    node* temp;
    for (i = 0; i < n - 1; i++)
        for (j = i; j < n; j++)
            if (a[i]->freq > a[j]->freq)
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
}
void sright(node* a[], int n)
{
    int i;
    for (i = 1; i < n - 1; i++)
        a[i] = a[i + 1];
}
void Assign_Code(node* tree, int c[], int n)
{
    int i;
    if ((tree->left == NULL) && (tree->right == NULL))
    {
        printf("%s code: ", tree->value);
        test[0][0]=tree->value;
        for (i = 0; i < n; i++)
        {
             test[0][i+1]=c[i];
            printf("%d", c[i]);
        }
        printf("\n");

    }
    else
    {
        c[n] = 1;
        n++;
        Assign_Code(tree->left, c, n);
              c[n - 1] = 0;
        Assign_Code(tree->right, c, n);
    }
}
void Delete_Tree(node * root)
{
    if(root!=NULL)
    {
        Delete_Tree(root->left);
        Delete_Tree(root->right);
        free(root);
    }
}
  • 1 1 Answer
  • 5 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:01:12+00:00Added an answer on May 13, 2026 at 11:01 pm

    As a side note:

      node* create(char a[], int x)
    {
        node* ptr;
        ptr = (node *) malloc(256*sizeof(node));  // <--- This is wrong
        ptr->freq = x;
        strcpy( ptr->value , a);
        ptr->right = ptr->left = NULL;
        return(ptr);
    }
    

    There is no reason to allocate 256 times the size of node to store one node. You are creating a node and storing it in an array of pointers to nodes. Allocate one node there, like this:

    malloc (sizeof (node)); or malloc ((sizeof (*ptr));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want declare on form as global variable and then work with that in
I have a header file variable.h where i declare all my global variable.Then i
I want to declare some global variables and global constants. Normally, I would put
I am using this Web worker which has a Global variable declared in it.
How to declare and use global variable with extern in Objective C and other
so how do you prefer to declare and use global variables? 1) global variable;
In C++, say you want to declare a global variable to be used by
If we declare a global variable in module in vb.net, we can use that
i have trying to use some global variable in my ant file. when i
If a function needs to modify a variable declared in global scope, it need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.