array initlisation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dschulenburg
    New Member
    • Oct 2006
    • 40

    array initlisation

    I fisrt define an array and then give values to its elements.

    float MATRIX2D_1[3][3]

    /*other stuff*/

    float MATRIX2D_1[3][3] = {1/9,-1/9,1/9}, {1/9,-1/9,1/9}, {1/9,-1/9,1/9}};

    I get a "parse error before float" for the second line. Does antone now why ?

    Thanks,

    Daniel
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    Each array can only be identified once. Get rid of the first declaration, since the second one has the data. Also you need an extra curly brace at the beginning, so the declaration is { {...} {...} {...} }.

    Comment

    • dynamicleo
      New Member
      • Oct 2006
      • 14

      #3
      You can use as below:
      Code:
      float MATRIX2D_1[3][3] = { {1/9,-1/9,1/9}, {1/9,-1/9,1/9}, {1/9,-1/9,1/9} };
      
      or
      
      float MATRIX2D_1[3][3] = { 1/9, -1/9, 1/9, 1/9, -1/9, 1/9, 1/9, -1/9, 1/9 };

      Comment

      • dschulenburg
        New Member
        • Oct 2006
        • 40

        #4
        Originally posted by D_C
        Each array can only be identified once. Get rid of the first declaration, since the second one has the data. Also you need an extra curly brace at the beginning, so the declaration is { {...} {...} {...} }.

        Thanks a lot (!)

        And if I want the matrix to be available throught the whole code, can I declare it as a macro (#define MATRIX2D_1 float arrayMRT[9][9]) or shall I define a globale varibale before the main() funtion (float MATRIX2D_1 arrayMRT[9][9];) ?

        Comment

        • iknc4miles
          New Member
          • Oct 2006
          • 32

          #5
          Originally posted by dschulenburg
          Thanks a lot (!)

          And if I want the matrix to be available throught the whole code, can I declare it as a macro (#define MATRIX2D_1 float arrayMRT[9][9]) or shall I define a globale varibale before the main() funtion (float MATRIX2D_1 arrayMRT[9][9];) ?
          If you're writing in C++, don't use #define for a global variable. As a macro, all #define does is replace MATRIX2D_1 with float. It takes the characters of the 1st argument, and replaces them with the characters of the 2nd argument.

          Better to define a global variable in C++ as:
          Code:
          //declare arrayMRT[9][9]
          public float arrayMRT[9][9];
          
          public int main()
          {
          //Initialize arrayMRT[9][9]
          arrayMRT[9][9] = { {....}{....}{....}{....}{....}{....}{....}{....}{....} };
          
          return 1;
          }
          Of course, you might want to avoid global variables alltogether but passing 2D arrays can be tricky.

          - Miles

          Comment

          • dschulenburg
            New Member
            • Oct 2006
            • 40

            #6
            OK, thanks. I am writting in C, how would you then do it ?


            Originally posted by iknc4miles
            If you're writing in C++, don't use #define for a global variable. As a macro, all #define does is replace MATRIX2D_1 with float. It takes the characters of the 1st argument, and replaces them with the characters of the 2nd argument.

            Better to define a global variable in C++ as:
            Code:
            //declare arrayMRT[9][9]
            public float arrayMRT[9][9];
            
            public int main()
            {
            //Initialize arrayMRT[9][9]
            arrayMRT[9][9] = { {....}{....}{....}{....}{....}{....}{....}{....}{....} };
            
            return 1;
            }
            Of course, you might want to avoid global variables alltogether but passing 2D arrays can be tricky.

            - Miles

            Comment

            • iknc4miles
              New Member
              • Oct 2006
              • 32

              #7
              Ok, the way I wrote it before should work for C. A friend of mine reminded me that "const" was not available in C which doesn't matter anyway if you want to change the values at some point.

              Try it out and see, I don't have a C compiler on me.

              - Miles

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by iknc4miles
                A friend of mine reminded me that "const" was not available in C
                const has been available in C since 1989 but is not as useful as it is in C++ because in some places in C it is still treated like a modifiable variable even though it is const (switch case statements for instance).

                Comment

                • iknc4miles
                  New Member
                  • Oct 2006
                  • 32

                  #9
                  Thanks for the info Banfa.

                  Never programmed in C if it isn't already obvious...

                  - Miles

                  Comment

                  Working...