Thread: error with Array of structure

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    error with Array of structure

    I am getting "initializer element is not constant" while compiling following program.

    In my source I want to keep list of all objects. I have created same scenario in the following program. Can you please help me to understand what went wrong with the following program.

    Code:
    struct ex_attribute {
            void (*fn1)(int);
            void (*fn2)(int, int);
    };
    
    
    void one(int a) {  return; }
    void two(int a,int b) { return; }
    void three(int a) { return; }
    void four(int a, int b) { return; }
    
    
    int main()
    {
            struct ex_attribute one1  = {
                    .fn1 = one,
                    .fn2 = two,
            };
    
    
            struct ex_attribute two2 = {
                    .fn1 = three,
                    .fn2 = four,
            };
    
    
            static struct ex_attribute total_attrs[] = {
                    one1,
                    two2,
            };
    
    
            return 0;
    }
    I also tried with array of structure pointers also as shown below but still I am getting the same error while compiling.

    Code:
            static struct ex_attribute *total_attrs[] = {
                    &one1,
                    &two2,
            };

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    struct ex_attribute {
            void (*fn1)(int);
            void (*fn2)(int, int);
    };
     
     
    void one(int a) {  return; }
    void two(int a,int b) { return; }
    void three(int a) { return; }
    void four(int a, int b) { return; }
     
     
    int main()
    {
            struct ex_attribute one1  = {
                    &one,
                    &two,
            };
     
     
             static struct ex_attribute total_attrs[] = 
             {
                {
                    &one,
                    &two
                },{
                    &three,
                    &four
                }
            };
     
     
            return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    Can you please tell what is wrong with my program.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by krkr View Post
    Can you please tell what is wrong with my program.
    one1 and two2 are not defined
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    Quote Originally Posted by vart View Post
    one1 and two2 are not defined
    one1 and two2 are of type "struct ex_attribute" and I have initialized these structure variables.

    Code:
          struct ex_attribute one1  = {
                    .fn1 = one,
                    .fn2 = two,
            };
    
    
            struct ex_attribute two2 = {
                    .fn1 = three,
                    .fn2 = four,
            };

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that since one1 and two2 are variables, they are not constants, so the compiler finds your initialisation of total_attrs unacceptable. You could just change it to:
    Code:
    static struct ex_attribute total_attrs[2];
    total_attrs[0] = one1;
    total_attrs[1] = two2;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    yes it is working. If I remove static keyword also for total_attrs then it is not giving any error.
    I think static keyword is related to lifetime and visibility, I am not getting clearly why static keyword is creating issue here.
    Any more properties for static keyword in C.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krkr
    yes it is working. If I remove static keyword also for total_attrs then it is not giving any error.
    I think static keyword is related to lifetime and visibility, I am not getting clearly why static keyword is creating issue here.
    Any more properties for static keyword in C.
    Yes, the static keyword here specifies that total_attrs has static storage duration. If by "visibility" you mean "scope", then no, the static keyword does not affect scope: despite having a lifetime that is the entire execution of the program, total_attrs is a local variable because it was declared in the scope of the main function. As for why the static keyword is an "issue":
    Quote Originally Posted by C11 Clause 6.7.9 Paragraph 4
    All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.
    This is presumably related to the fact that initialisation for objects with static storage duration happens prior to program startup, so total_attrs cannot be initialised with one1 and two2 since one1 and two2 do not exist prior to program startup.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    Thanks for the information.

    Is function names in C are constants?


    I have defined a structure having function pointers, Created a static object for that and initialized function pointers. This program compiled without any issues.


    Code:
    struct attribute {
            const char              *name;
    };
    
    
    struct one_attribute {
            struct attribute atr;
            int (*f1)();
            int (*f2)();
    };
    
    
    int ex_f1()
    {   return 0;  }
    
    
    int ex_f2()
    {   return 0;  }
    
    
    static struct one_attribute ex_attr_cnt = {
            .atr = { .name = "cont"},
            .f1 = ex_f1,
            .f2 = ex_f2,
    };
    
    
    static struct attribute *names[] = {
            &ex_attr_cnt.atr,
    };
    
    
    int main()
    { return 0; }

  10. #10
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    Quote Originally Posted by laserlight View Post
    Yes, the static keyword here specifies that total_attrs has static storage duration. If by "visibility" you mean "scope", then no, the static keyword does not affect scope: despite having a lifetime that is the entire execution of the program, total_attrs is a local variable because it was declared in the scope of the main function. As for why the static keyword is an "issue":

    As per my understanding scope means a region in code where a variable can be accessed. If we declare a global variable as static then its scope is limited to that file. so using global variable as static will affect the variable scope to that file or translation unit.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krkr
    As per my understanding scope means a region in code where a variable can be accessed. If we declare a global variable as static then its scope is limited to that file. so using global variable as static will affect the variable scope to that file or translation unit.
    If we declare a global variable as non-static then its scope is also limited to that file (or translation unit). Whether the variable is declared static or not, it has file scope. The difference is that if it is declared static, then it has internal linkage, otherwise it has external linkage. With external linkage, a variable at file scope with the same name in another file refers to the same object, whereas with internal linkage that other variable refers to a different object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    I have initialized a static structure with function name as shown below. I think we can initialize a static structure with constants.
    function names are constants in C?

    Code:
    struct fp {
            int (*fn)();
    };
    
    int f1()
    {
            printf("f1 called \n");
            return 0;
    }
    
    static struct fp fps = {
            .fn = f1,
    };
    
    int main()
    {
            fps.fn();
            return 0;
    }
    When I initialized structure as shown below then also it is working fine.

    Code:
    static struct fp fps = {
            .fn = &f1,
    };
    Is "f1" and "&f1" is same in C.

  13. #13
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    C compiler will traat function names as string constants?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krkr
    Is "f1" and "&f1" is same in C.
    For a function named f1, yes.

    Quote Originally Posted by krkr
    C compiler will traat function names as string constants?
    No, function names are not string constants. Rather, a function (or more precisely, function designator) is converted to a pointer to the function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    Quote Originally Posted by laserlight View Post

    No, function names are not string constants. Rather, a function (or more precisely, function designator) is converted to a pointer to the function.
    If function name is not conatant then following program should throw error, because initializers should be constants for static objects. But it is compiling successfully.

    Code:
    struct ex {
            void (*fp)();
    };
    void f1()
    {
            return ;
    }
    static struct ex temp = {
            .fp = f1,
    };
    int main()
    {
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-07-2015, 11:19 AM
  2. structure typedefs error
    By pobri19 in forum C Programming
    Replies: 1
    Last Post: 02-03-2009, 03:44 AM
  3. Help with error in function with structure
    By DiscoStu9 in forum C Programming
    Replies: 14
    Last Post: 10-20-2008, 02:29 PM
  4. Error on scanf with structure
    By George M. in forum C Programming
    Replies: 14
    Last Post: 09-20-2008, 12:03 PM
  5. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM

Tags for this Thread