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 7793015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:21:13+00:00 2026-06-01T22:21:13+00:00

Im building a symbol table and im having a hard time writing get_symbol, which

  • 0

Im building a symbol table and im having a hard time writing get_symbol, which has the arguments of (symbol_table symtab, char sym). I have to write code for 2 functions get_symbol and set_symbol, I’m having some trouble understanding what a symbol table is and how to write out these two functions. it says that get_symbol() has two arguments: the symbol table, and the symbol name whose value should be retrieved. If the symbol name is one of A,B,C the corresponding value from the symbol table should be returned. if it is not one of those names, then 0 should be returned. If any one could help that would be great thanks for your time.

symbol_table.c Code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #include "globals.h"
    #include "symbol_table.h"

    typedef struct symbol_table_s {
            int a,b,c;
    }*symbol_table_rep;


    status init_symbol_table (symbol_table *p_symtab)
    {
    symbol_table_rep st = malloc(sizeof(struct symbol_table_s));
    if (st == NULL)
            return ERROR;
    st->a = st->b = st->c = 0;
    *p_symtab = (symbol_table) st;
    return OK;
    }

    void destroy_symbol_table (symbol_table *p_symtab)
    {
    free(*p_symtab);
    *p_symtab = NULL;
    }
    void set_symbol(symbol_table *p_symtab, char sym, int value)
    {

    /* WRITE CODE FOR THIS */

    }
    int get_symbol (symbol_table symtab, char sym)
    {

    /* WRITE CODE FOR THIS FUNCTION */


    symbol_table_rep symtab;
    if (A,B,C)
    {
            return symbol_table;
    }else{
            return 0;
    }
    }

    symbol_table.h Code:
    #ifndef _SYMBOL_TABLE_H
    #define _SYMBOL_TABLE_H

    #include "globals.h"

    ABSTRACT_TYPE(symbol_table);

    status init_symbol_table   (symbol_table *p_symtab);
    void   destroy_symbol_table(symbol_table *p_symtab);

    void   set_symbol          (symbol_table *p_symtab, char sym, int value);
    int    get_symbol          (symbol_table  symtab,    char sym);

    #endif
  • 1 1 Answer
  • 0 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-06-01T22:21:15+00:00Added an answer on June 1, 2026 at 10:21 pm

    There are a number of other problems with your code, not least that you are passing the entire symbol table by value into get_symbol. How much of the code you’re showing did you write, and how much is boiler-plate code you’re supposed to leave as-is?

    Here is an implementation of a symbol table where the symbols are single character and the values are just ints, as with your code. But this supports more than 3 symbols.

    // symbol_table.h
    struct symbol_table;
    struct symbol_table* init_symbol_table(void);
    void   destroy_symbol_table(struct symbol_table *p_symtab);
    
    void   set_symbol          (symbol_table *p_symtab, char sym, int value);
    int    get_symbol          (const symbol_table  *symtab,    char sym);
    
    // symbol_table.c
    #include <limits.h>
    #include <stdlib.h>
    #define ARRAYSIZE(a) (sizeof(a)/sizeof((a)[0]))
    struct symbol_table
    {
      // On rare systems, UCHAR_MAX == SIZE_MAX, and this array size will not work.
      // Most people will never write code for such a system though.  We'll ignore them. 
      int values[UCHAR_MAX+1];
    };
    
    struct symbol_table* init_symbol_table (void)
    {
      struct symbol_table *p = malloc(sizeof(struct symbol_table));
      if (p) 
      { 
        size_t i;
        for (i=0; i<ARRAYSIZE(p->values); ++i)
          p->values[i] = 0;
      }
      return p;
    }
    
    
    void destroy_symbol_table(struct symbol_table *p)
    {
      free(p);
    }
    
    void   set_symbol (symbol_table *p, char sym, int value)
    {
      p->values[(unsigned char)sym] = value;
    }
    
    int get_symbol (const symbol_table  *p, char sym)
    {
      return p->values[(unsigned char)sym];
    }
    

    If you need to keep the function interface identical (complete with the rather bizarre definition of symbol_table) then you can just implement get_symbol and set_symbol with some simple conditional statements: either a sequence of if statements or a switch statement.

    If you’re having difficulty with that then go re-read the parts of your course materials which deal with character types and if. If your course materials don’t cover that, then you should find some other resources for learning the C language; try starting with the items mentioned at Great C tutorial?

    Yes, I could write the get_symbol and set_symbol code for you but I believe the help you’re looking for is more around figuring out how to get started with the problem rather than a getting a finished result without understanding.

    The key thing I believe you need to achieve is to get a detailed understanding of what specific actions the computer needs to take to return the value of one of the symbols. Start by stating that, as precisely as you can, in any notation at all (a diagram, or in English, whatever). Then try to implement that understanding in the C language.

    This process of first understanding the mechanism by which you will solve the problem – that is, what specifically you want to make the computer do – is totally central to the process of learning to program. That learning experience is the thing this kind of homework is intended to provide, I guess. But nobody can do it for you. Showing you completed code might not help, because it won’t push into your head the “Aha!” insight.

    If you’re really, totally, stuck, start with this implementation:

    void set_symbol(symbol_table *p_symtab, char sym, int value)
    {
      /* WRITE CODE FOR THIS LATER */
    }
    
    int get_symbol (symbol_table symtab, char sym)
    {
      return 0;
    }
    

    It clearly does the wrong thing, but it will compile. Then work on it by modifying it to return a fixed value for A, for B, and for C. Then refer to your learning materials about how to access the members of a structure. Change the code to always return the value of the a member. Then try to figure out how to distinguish between the cases where the caller wants to fetch the value of A or B or C. You might find it helpful to ‘artifically’ set the members a b and c of the symbol table to some characteristic value to make sure you’re returning the correct one (but remember to remove that code later).

    Once you have done that, start working on implementing set_symbol. By the time you have made get_symbol work, set_symbol should be easy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a RPG character generator and am having a semi-difficult time with
I'm building a symbol table for a project I'm working on. I was wondering
I am writing Catalyst application which uses Module::Install for module building. I keep all
Building a website that has English & Japanese speaking users, with the Japanese users
When building a Delphi 2009 component package, how do you specify which directory should
Under Mac OS X 10.6 I am building a C++ shared library which links
All, I'm in the process of building a web application (C#, MVC3 Razor) which
I've been getting this undefined symbol building with this command line: $ gcc test.cpp
I am building a binary which uses multiple components which are compiled as .so
I am building a website which involves user comments and tags. I wish to

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.