1

I have the following code:

typedef struct A_{

  void* values;
}A;

typedef struct C_{

  int array_C[3][3];
}C;

typedef struct B_{

  C c;
}B;

int main(void)
{
    B* b;

    A array_A[2] = {
                        {
                            &(b->c.array_C)   \\ line 23
                        },

                        {
                            &(b->c.array_C)    \\ line 27
                        }
                  };

   return 0;
}

my error is in line 23 and 27 which says that &(b->c.array_C) should be const. what I am doing wrong ? adress of an array is not a const?

The reason that i am doing it is because i need to improve cyclomatic complexity of a function with 340 if statement, when each if statement is with the following form:

if( exspretion )
{
   foo( address_of_array);
   return false;
}

if i will change the 340 if statements to one while loop, which runs 340 times and get from the above array the argument for foo function the cyclomatic complexity will be better ?

IceCube
  • 405
  • 1
  • 5
  • 12
  • 2
    The problem is the expression `&(b->c.array_C)` requires the value of `b` to be known at compile time, which it is not. – Drew McGowen Jul 28 '14 at 20:56
  • 2
    `b` is uninitialized pointer. You should initialize it with a valid address first. – 0xF1 Jul 28 '14 at 20:57
  • Would this change really improve the clarity of the code, or will it just fool the tool into thinking the complexity is simpler by hiding the real code in an array? – Moby Disk Jul 28 '14 at 21:08
  • @DrewMcGowen Why does the expression require the value of `b` to be known at compile time? – Utkan Gezer Jul 28 '14 at 21:29
  • @ThoAppelsin simply going off of the "should be const" warning OP mentioned – Drew McGowen Jul 28 '14 at 21:30
  • In code, create both an instance of B, and a pointer to B, on the next line, initialize the pointer by setting it equal to the address of the instance. (See code below, too messy for a comment). – ryyker Jul 28 '14 at 22:18
  • Of course the cyclomatic complexity would be reduced if 340(!) if statements were replaced by a single for with a single if inside the loop, but that would basically make the function a (very basic) interpreter of the large array. Another approach you could take would be to generate the code - e.g. using a Perl script, from a csv file. My question for you would be why you want to reduce the cyclomatic complexity in the first place? Code clarity? – rlc Jul 28 '14 at 23:41
  • @Andrey Isakov: What compiler are you using? The "should be const" requirement is only present in C89/90. And very few C89/90 C compilers actually enforced it (frankly, I don't now a single one that would do it by default). There wouldn't be such error in C99 (or later) C compiler. It it a requirement to stick to archaic C89/90 specification in your case? – AnT stands with Russia Dec 05 '16 at 19:38
  • @ThoAppelsin: It is not required to be known at compile time in modern C. But in C89/90 it was. All `{}`-enclosed initializers in C89/90 were required to consist of compile-time constants (even when used with local objects). This requirement was overthrown in C99. But most C89/90 compilers ignored that requirement already, which makes me wonder what compiler the OP is using. – AnT stands with Russia Dec 05 '16 at 19:43

1 Answers1

1

To address the error you are seeing, the line:

B* b;  

Can be changed to:

B b, *pB;//create an instance of B, and a pointer to B at the same time. 
pB = &b; //now, pB is initialized to point at a real place in memory. (i.e. b)  
         //(use it instead of b in your code)  

Main would now look like this:

int main(void)
{
    B b, *pB;
    pB = &b;

    A array_A[2] = {
                        {
                            &(pB->c.array_C)   // line 23
                        },

                        {
                            &(pB->c.array_C)    // line 27
                        }
                  };
   //assignments can now be made to the actual array elements  
   pB->c.array_C[0][0]=3;//for example

   //Question:  How are you planning on using   array_A[0], array_A[1]?  (A is a void *)

   return 0;
}  

Regarding cyclomatic complexity
and your question:
...if i will change the 340 if statements to one while loop... the cyclomatic complexity will be better ?

cyclomatic complexity of a section of source code is the count of the number of linearly independent paths through the source code. For instance, if the source code contained no decision points such as IF statements or FOR loops, the complexity would be 1, since there is only a single path through the code. (From HERE))

A for loop would not be an improvement over 340 sequential calls in terms of cyclomatic complexity, but in terms of maintainability and readability, it would be a big improvement.

ryyker
  • 22,849
  • 3
  • 43
  • 87