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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:47:51+00:00 2026-05-14T14:47:51+00:00

This probably is one of the easiest question ever in C programming language… I

  • 0

This probably is one of the easiest question ever in C programming language…

I have the following code:

typedef struct node
{
  int data;
  struct node * after;
  struct node * before;
}node;

struct node head = {10,&head,&head};

Is there a way I can make head to be *head [make it a pointer] and still have the availability to use ‘{ }’ [{10,&head,&head}] to declare an instance of head and still leave it out in the global scope?

For example:

 //not legal!!!
 struct node *head = {10,&head,&head};
  • 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-14T14:47:52+00:00Added an answer on May 14, 2026 at 2:47 pm

    Solution 1:

    #include <stdlib.h>
    #include <stdio.h>
    
    
    typedef struct node
    {
      int data;
      struct node * after;
      struct node * before;
    }node;
    int main() {
    
        struct node* head = (struct node *)malloc(sizeof(struct node)); //allocate memory
        *head = (struct node){10,head,head}; //cast to struct node
    
        printf("%d", head->data);
    
    }
    

    Something as simple as struct node *head = {10, head, head} is not going to work because you haven’t allocated the memory for the struct (the int and two pointers).

    Solution 2:

    #include <stdlib.h>
    #include <stdio.h>
    
    
    typedef struct node
    {
      int data;
      struct node * after;
      struct node * before;
    }node;
    int main() {
    
        struct node* head = &(struct node){10,head,head};
    
        printf("%d", head->data);
    
    }
    

    This will go out of scope – Solution 1 is superior for this reason and since you’re creating a linked list, I believe you need heap allocated memory – not stack allocated.

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

Sidebar

Related Questions

This is probably a pretty high-level question that requires a lot of explaining, but
This is probably something very simple but I'm not getting the results I'm expecting.
Being probably one of the worst OOP programmers on the planet, I've been reading
I have a Google Map on one web page where I want to disable
This is probably simple but I do not seem to get it to work.
I have a number of websites that are rendering invalid characters. The pages' meta
I have a bunch of Python modules I want to clean up, reorganize and
I'm having a problem with either typecasting or object scope. I'm getting an uncaught
I'm coding something at the moment where I'm taking a bunch of values over
I've written a little Ajax-y page, that displays data rows from an ajax request.

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.