Thread: run-time var initialization

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    run-time var initialization

    In plain C, when initializing a matrix you can do it:
    int m[2][2] = { {2,3} , {3,4} };
    but I am trying to do it at run time:


    Code:
    int F[5][8];
    
    int main()
    {
    	int x= 1;
    	F[x] = {-6, -3, 0, 4, 8, 6, 2, -4 };
    	int x= 2;
    	F[x] = {-7, -3, 0, 5, 8, 6, 2, -8 };
            .....
        return 0;
    }
    And I get compiling errors. It looks like this way is not allowed.

    Is there any way to inialize a matriz in bulk, so that there is no need to initialize values one by one.

    thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    int m[2][2] = { {2,3} , {3,4} };
    It's already initialization in bulk.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by Bayint Naung View Post
    int m[2][2] = { {2,3} , {3,4} };
    It's already initialization in bulk.
    That is not what I want. suppose I want to initialize it depending on a var:

    Code:
    int F[5][8];
    
    int main()
    {
    	if (z == 18) {
    	    F[1] = {-6, -3, 0, 4, 8, 6, 2, -4 };
            } else {
    	    F[1] = {-7, -3, 0, 5, 8, 6, 2, -8 };
            }
            .....
        return 0;
    }
    I can not do this also. any idea or I must do it value by value?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    	
            if (z == 18) {
             int temp[] =  {-6, -3, 0, 4, 8, 6, 2, -4 };   
             /* of course you can use 
            #define  INIT_ARRAY  { -6, -3,0,blah}  int temp[] = INIT_ARRAY;  
             */
                 // copy 
                memcpy(f[1],temp,sizeof(temp));
            } else {
                int temp[] = {-7, -3, 0, 5, 8, 6, 2, -8 };
               // similar
            }
    Or use variadic macro. Example (Not suppose to be perfect
    Code:
    #define INIT_ARRAY(array,type,...)    \
    do {                                                                    \
          memcpy(array,  ( type [ sizeof(array)/sizeof(array[0]) ]) { __VA_ARGS__ }, sizeof(array) );   \
    } while(0)
    INIT_ARRAY(f[1],int, 3,4,5 );     // eg usage
     } while(0)
    Last edited by Bayint Naung; 06-23-2010 at 03:19 AM.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    macro? memcpy? stay away

    just make a new array and copy over the contents

    Code:
            if (z == 18) {
                        int temp[] = {-6, -3, 0, 4, 8, 6, 2, -4 };
    	    for(...) {
                            F[1][i] = temp[i]
                        }
            } else {
    just some general advice:
    when writing a program, the first thing you should do is get something that works and is correct. then go back and do whatever you feel is required. i think you knew exactly how to get around this problem by using a temp array and i dont think you should have spent any more than a few seconds trying to think of better method. the temp array solution was simple, obvious and clear.

    its like when you are writing an essay at home. the first thing you should do is write it all out. then you should go and choose fonts, choose header styles, colors, pictures, etc. but only after youve finished

    and use spaces, not tabs. if you must use tabs, then dont mix and match with spaces.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Run time error in linkList attemp
    By prominababy in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2009, 08:06 AM
  2. Run time error?
    By shwetha_siddu in forum C Programming
    Replies: 6
    Last Post: 04-21-2009, 01:37 PM
  3. How to time how long a program takes to run?
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 08-18-2008, 07:50 PM
  4. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 09:24 AM