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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:44:57+00:00 2026-06-15T04:44:57+00:00

1 #include<stdio.h> 2 #include<malloc.h> 3 4 typedef struct node_t{ 5 int i; 6 struct

  • 0
  1 #include<stdio.h>
  2 #include<malloc.h>
  3 
  4 typedef struct node_t{
  5     int i;
  6     struct node_t* link;
  7 }node;
  8 
  9 node* head = (node *)malloc(sizeof(node));
 10 
 11 if(head == NULL){
 12     printf("\n malloc for head node failed! \n");
 13 }
 14 
 15 int main(){
 16     int i = 10;
 17     node* temp = NULL;
 18     temp = (node *)malloc(sizeof(node));
 19     if(temp == NULL){
 20         printf("\n malloc for temp node failed! \n");
 21     }
 22     else{
 23         while(i<=10){
 24             ;
 25         }
 26     }
 27     return 0;
 28 } 

compilation error:

linked.c:9:1: error: initializer element is not constant
linked.c:11:1: error: expected identifier or ‘(’ before ‘if’

I’m trying a simple linked list programme. It’s not fully completed. I’m getting a compilation error. Couldn’t understand why this happened.

  • 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-15T04:44:58+00:00Added an answer on June 15, 2026 at 4:44 am

    Since you’re defining head as a global, its initializer needs to be a constant–basically, the compiler/linker should be able to allocate space for it in the executable, write the initializer into the space, and be done. There’s no provision for calling malloc as you’ve done above during initialization–you’ll need to do that inside of main (or something you call from main).

    #include <stdlib.h>
    
    void init() { 
        head = malloc(sizeof(node));
    }
    
    int main() { 
        init();
        // ...
    }
    

    In this case, the code you have in main never actually uses head though, so you may be able to skip all of the above without a problem.

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

Sidebar

Related Questions

#include<stdio.h> #include<malloc.h> typedef struct node_t { int i; struct node_t* link; } node; node*
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
#include <stdio.h> typedef struct node { int i; struct node *next; }node; node getnode(int
#include <stdio.h> void wrapperPrint(char* s) { printf(s); return; } int main() { wrapperPrint(Hello world\n);
#include<stdio.h> #define N (sizeof(array) / sizeof(array[0])) main() { int array[5]={1,2,3,4,5}; int d; for(d=-1;d <=
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; void insert( struct
Here's my code: #include <stdio.h> typedef struct node_struct { int data; struct node_struct *next;
#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
#include <stdio.h> #include <pthread.h> #include <time.h> #include <stdlib.h> typedef struct pr_struct{ int owner; int
#include <stdio.h> #include <stdlib.h> typedef int element; struct cell { element e; struct cell

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.