MISRA Discussion Forums

Full Version: Does this code violate rule #8.7?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Given this example
Code:
file1.c:
        static int i;
        int j[1] = { i };
file2.c:
        extern int j[1];
        void f()
            {
            int k = j[0];
            }
Does this code violate rule #8.7, "Objects shall be defined at block scope if they are only accessed from within a single function"?
The code fragment in file1.c needs a little more context in order to answer the question.

Is the intention that the definition of j occur outside of any block?

The initialisation of j seems to violate constraints because initialisers for objects with static storage duration must be constant expressions.

Please could you amend the example?
Suppose the types of 'i' and 'k' in this example were pointers to functions; would such make any difference?
There is still insufficient information to answer the question fully. I have sent a private message to gs asking for clarification. Please can you read and reply to this?