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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:23:25+00:00 2026-06-15T20:23:25+00:00

I am new to C and I am currently implementing a Scheme interpreter in

  • 0

I am new to C and I am currently implementing a Scheme interpreter in C. I am close to the end but a problem is bothering me which I have not been able to tackle yet.

I want a “globalEnvironment” pointer to a struct which stays throughout the time the program runs and gets modified too (not a constant).

/****************************************************************
 Creates the List as a pointer to the conscell structure
 */
typedef struct conscell *List;

/****************************************************************
 Creates a conscell structure, with a char pointer (symbol) and two pointers to
 * conscells (first and rest)
 */
struct conscell {
    char *symbol;
    struct conscell *first;
    struct conscell *rest;

};


List globalEnvironment;


/****************************************************************
 Function: globalVariables()
 --------------------
 This function initializes the global variables
 */
void globalVariables() {

globalEnvironment = malloc(sizeof (struct conscell));
globalEnvironment->symbol = NULL;
globalEnvironment->first = NULL;
globalEnvironment->rest = NULL;

}

As you can see “List” is a pointer to a conscell structure. So all I want is the globalEnvironment List to be global.

The problem is that I cannot do malloc there. If I try the following:

List globalEnvironment = malloc(sizeof (struct conscell));

instead of just “List globalEnvironment;” it gives an error that “initialiser element is not a constant”

To tackle this situation, I created a new function “globalVariables” which runs at the beginning of the program, initialises globalEnvironment and allocates it memory.
It is not working as I expected though and I keep getting segmentation fault errors for other functions that I have not written here to keep it simple.

Is there another, simpler, way to declare a pointer (not constant) to a structure in C?

Hope someone can help,
Thank you

  • 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-15T20:23:27+00:00Added an answer on June 15, 2026 at 8:23 pm

    It looks like you are trying to malloc when you should just use global data. You could try

    struct conscell globalEnvironment;
    

    Just remember to never free it.

    If you need to have a pointer handle so you can push cells on the list:

    struct conscell _globalEnvironment;
    List globalEnvironment = &_globalEnvironment;
    

    Still, remember to never free _globalEnvironment.

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

Sidebar

Related Questions

We're currently looking at implementing Facebooks new subscription payments . We already have a
I've got a problem which I can't seem to solve. I'm currently implementing a
I'm currently poking around with a new archive format and was interested in implementing
I'm currently facing new problem with operators. Using following code, I want to make
Im new to objC and Im currently experimenting on UISearchDisplayController. Basically I have an
I am currently implementing cache. I have completed basic implementation, like below. What I
I'm currently implementing a new functionality to a tool in an e-learning platform. I
I'm currently implementing uml validation http://msdn.microsoft.com/en-us/library/ee329482.aspx , when i debug, it opens a new
I'm currently implementing a custom MultiPageEditorPart where I want to have multiple pages when
I'm currently implementing functionality in Tridion 2009 SP1 where the user is able 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.