Thread: Initializing 2D array in Middle of program

  1. #1
    Registered User subhadeepgayen's Avatar
    Join Date
    Jul 2010
    Posts
    9

    Initializing 2D array in Middle of program

    Code:
    for(i=0;i<strlen(string);i++)
        {
           switch(string[i])
           {
              case 'A':
              case 'a':
                   digits[i][6]={1,0,1,0,1,0};   <<<<<<<<<< DA PROBLEM
                   break;
           }
        }
    Hi i know how to initialize a 2d array in start of program, but i want to initialize in middle, how to do that, the above program gives error.

    The only solve which worked is:
    digits[i][0]=1;
    digits[i][1]=0;
    digits[i][2]=1;
    digits[i][3]=0;
    digits[i][4]=1;
    digits[i][5]=0;

    how can i do that in one go?

    P.S Sorry for bad English.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't do it in one go. You can either initialize it at the beginning or assign it in multiple steps later.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't do what you are trying, but note that every odd index in digit[] is 0, and every even index is 1. (In the second dimension). So a loop using if(j%2==0), should work ok, where j = 2nd dimension index.

  4. #4
    Registered User subhadeepgayen's Avatar
    Join Date
    Jul 2010
    Posts
    9
    Thanks for clearing me up.
    @adak thanks for further suggestions but those are just testing values, so it wont be alternating for final one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Filling a 2d Array cause program to crash
    By Geo-Fry in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2003, 07:00 AM