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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:51:36+00:00 2026-06-09T00:51:36+00:00

The folowing piec of code generates error: initializer element is not constant at compile

  • 0

The folowing piec of code generates error: initializer element is not constant
at compile time on the line declaring and initializing the user struct variable.

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

struct user_s {
    char *name;
    void (*(*pred_skip_func))(int);
};

void f1 (int skip) {
    printf("I am f1\n");
}

void f2 (int skip) {
    printf("I am f2\n");
}

void (*(*pred_skip_func))(int);
struct user_s user = {"Manu", pred_skip_func};

int main(void) {

    struct user_s tmp;
    pred_skip_func = malloc(sizeof(tmp.pred_skip_func) * 2);
    pred_skip_func[0] = f1;
    pred_skip_func[1] = f2;

    int i;
    for (i = 0; i < 2; i++) {
        (*(user.pred_skip_func)[i]) (i);
    }
    return EXIT_SUCCESS;
}

Moving the initialization in the main function solves the issue, but I want to understand why ? Is there any restriction on structure initialisation ?

More over, as you can see, I created a tmp user_struc variable to get the size of my pointer to function pointers because I was not able to do this in a cleaner way. How can I fix this ?

  • 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-09T00:51:39+00:00Added an answer on June 9, 2026 at 12:51 am

    First question:

    “Is there any restriction on structure initialisation ?”

    C requires initializers for aggregate types with static storage duration to be constant:

    (C99, 6.7.8p4) “All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.”

    Note that in C89 even if the object of the aggregate type had automatic storage duration the intializers had to be constant expressions (this is no longer the case in C99).

    Second question:

    “More over, as you can see, I created a tmp user_struc variable to get the size of my pointer to function pointers because I was not able to do this in a cleaner way.”

    You can use your user object to compute the size of the member:

    sizeof (user.pred_skip_func)
    

    or use a C99 compound literal if you have not declared any object of the structure type:

    sizeof (((struct user_s) {0}).pred_skip_func) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The folowing code generates a syntax error at the line where the iterator is
I'm getting the folowing error for below code, 1506-221 (S) Initializer must be a
I Have folowing Button code and setting to take user to google for login
have folowing code: NPVariant type; STRINGZ_TO_NPVARIANT(click, type); and the xcode returns error: expected expression
The folowing program was orignally in java. But i still get 1 error with
My question is the folowing Is there a solid javascript code to detect if
I have folowing code in which i am using some conditions on page to
I'm folowing this tutorial to create php/jquery based chat application. In short, this code
I added folowing line to my MasterTableView: <CommandItemSettings ShowExportToWordButton=true ShowExportToExcelButton=true ShowExportToCsvButton=true /> But when
I'm using the folowing code to read files from a folder in windows. However

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.