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

  • Home
  • SEARCH
  • 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 5962903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:10:17+00:00 2026-05-22T19:10:17+00:00

I am writing a software in C. For that purpose I use lex. I

  • 0

I am writing a software in C. For that purpose I use lex. I wrote a piece of code in C to create a symbol table and manage it. So, whenever lex finds a new symbol, it puts it in a symbol table. Problem is, when I try to print all results from symbol table, I get output I didn’t expect.
If, for example, the input file was:

int main(){}

the output should be:

int
main
(
)
{
}

but the output is:

int main(){}
main(){}
(){}
...

and so on.
The function used for printing is something like this

void print_entries(struct symtab *start) {
   struct symtab *s = start;
   while(s != NULL) {
      printf("%s\n", s->name);
      s = s->next;
   }
}

Here is the code for adding new symbols:

void add_entry(char* name, int type, struct symtab *start)
{
   struct symtab *new;
   new = malloc(sizeof(struct symtab));
   last_entry(start)->next = new;
   new->name = name;
   new->type = type;
   new->next = NULL;
}

Any ideas?

  • 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-05-22T19:10:17+00:00Added an answer on May 22, 2026 at 7:10 pm

    You need to copy the symbol names into the symbol table entries. If for some peculiar reason your system does not have strdup() already, then use:

    #include <string.h>
    #include <stdlib.h>
    
    char *strdup(const char *str)
    {
       size_t len = strlen(str) + 1;
       char *dup = malloc(len);
       if (dup != 0)
           memmove(dup, str, len);
       return dup;
    }
    

    (In this context, I could use memcpy() safely; I use memmove() because it always works and memcpy() does not. And I use memmove() because I know exactly how long the string is so the copy doesn’t need to test each character for nullness as it goes.)

    With strdup() on hand:

    void add_entry(char* name, int type, struct symtab *start)
    {
       struct symtab *sym;
       sym = malloc(sizeof(struct symtab));
       last_entry(start)->next = sym;
       sym->name = strdup(name);
       sym->type = type;
       sym->next = NULL;
    }
    

    Note that this still omits the error checking from the two memory allocations, which is not a good habit to get into. I’ve revised it to use sym rather than new because the latter is a C++ keyword and I avoid using those as identifiers, even in C code.

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

Sidebar

Related Questions

I'm writing a piece of open-source client software that can connect to web services
I installed linux (Debian) on an old laptop and have been writing an OpenGL
I have a database in a state own ms sql server. Writing user information
Well, I've looked all over the internet, and it appears that what I want
I'm a software developer. The only browser I have at work is IE7. I
I am assigned to design a business system. The requirement of the client is
I am using a motor connected to a serial port (RS-232). I need to
I've been trying to track down why my Office2010 plugin leaves a null pointer
I have started to write a Python 3.x client application. The server application exists

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.