Fully Initializing an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alfons
    New Member
    • Dec 2007
    • 6

    #1

    Fully Initializing an array

    Hey,

    In TC/C++

    I know that if we intialize just the 1st digit of an Integer Array ( int a[3][3]={1} )
    its output with will be:
    100
    000
    000

    But what if we want the output of a 2-D array a[3][3] like that.
    ***
    ***
    ***
    Or with any other character or number .
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Well, if your array holds characters rather than integers, perhaps you should make it a char array?

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      You can do it like:

      [CODE=cpp]int a[3][3]={{0,0,0},{0,0, 0},{0,0,0}};

      //or

      int a[3][3]={0,0,0,0,0,0,0 ,0,0};[/CODE]

      same with chars you can do:

      [CODE=cpp]char a[3][3]={{"ab"},{"cd"} ,{"ef"}}


      //or

      char a[3][3]={{'a','b'},{'c ','d'},{'e','f' }};[/CODE]

      and two more combinations without inner '{' and '}'.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        And you migh as well read this:
        Originally posted by weaknessforcats
        First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
        [code=c]
        int array[5];
        [/code]

        That is an array of 5 elements each of which is an int.

        [code=c]
        int array[];
        [/code]

        won't compile. You need to declare the number of elements.

        Second, this array:
        [code=c]
        int array[5][10];
        [/code]

        is still an array of 5 elements. Each element is an array of 10 int.

        [code=c]
        int array[5][10][15];
        [/code]

        is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


        [code=c]
        int array[][10];
        [/code]

        won't compile. You need to declare the number of elements.

        Third, the name of an array is the address of element 0
        [code=c]
        int array[5];
        [/code]

        Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

        [code=c]
        int array[5][10];
        [/code]

        Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
        [code=c]
        int array[5][10];

        int (*ptr)[10] = array;
        [/code]

        Fourth, when the number of elements is not known at compile time, you create the array dynamically:

        [code=c]
        int* array = new int[value];
        int (*ptr)[10] = new int[value][10];
        int (*ptr)[10][15] = new int[value][10][15];
        [/code]

        In each case value is the number of elements. Any other brackets only describe the elements.

        Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

        [code=c]
        int** ptr = new int[value][10]; //ERROR
        [/code]

        new returns the address of an array of 10 int and that isn't the same as an int**.

        Likewise:
        [code=c]
        int*** ptr = new int[value][10][15]; //ERROR
        [/code]

        new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

        With the above in mind this array:
        [code=cpp]
        int array[10] = {0,1,2,3,4,5,6, 7,8,9};
        [/code]
        has a memory layout of

        0 1 2 3 4 5 6 7 8 9

        Wheras this array:
        [code=cpp]
        int array[5][2] = {0,1,2,3,4,5,6, 7,8,9};
        [/code]
        has a memory layout of

        0 1 2 3 4 5 6 7 8 9

        Kinda the same, right?

        So if your disc file contains

        0 1 2 3 4 5 6 7 8 9

        Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

        Therefore, when you do your read use the address of array[0][0] and read as though you have a
        one-dimensional array and the values will be in the correct locations.

        Comment

        • alfons
          New Member
          • Dec 2007
          • 6

          #5
          Yea..
          But the catch is that i want to initialize a single entry , so that it can fill it up with the same .
          like i did with the 0 one

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Then you will need to write code for that.

            Comment

            Working...