questions about arrays

This is a discussion on questions about arrays within the C++ Programming forums, part of the General Programming Boards category; I've been playing around a bit with arrays and a few question have arisen that I hope someone on here ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    questions about arrays

    I've been playing around a bit with arrays and a few question have arisen that I hope someone on here can help answer.

    1. Sizeof
    Code:
    void foo( int array[] )
    {
        cout << sizeof(array) << endl;
        cout << sizeof(array[0]) << endl;
    }
    
    int main()
    {
        int b[4] = {1,2,3,4};
    
        cout << sizeof(b) << endl;
        cout << sizeof(b[0]) << endl;
        cout<<"---------" << endl;
        foo(b);
    }
    The above outputs
    16
    4
    -----
    4
    4
    Im just curious why they the output from main is different from the foo() method ?

    2. The below code both store the values 1,2,3,4 just the same way in memory.
    Code:
    int a[2][2] = {{1,2}, {3,4}};
    int b[4] = {1,2,3,4};
    if I do
    Code:
    int* ptr = b;
        
    for(int x=0; x < 4; x++)
        cout << *ptr++ << endl;
    that works great, however if I do
    Code:
    int* ptr = a;
        
    for(int x=0; x < 4; x++)
        cout << *ptr++ << endl;
    I get an 'cannot conver int' error on the first line. I though both a and b array where const pointers to the first element of each array and then there shouldnt be an problem copying the address into a pointer and using the pointer to access the values, but this doesnt seem to work so I guess it's not that simple. Would appreciate if anyone could shed some light on this.

    3. If I have the following main code..
    Code:
    int main()
    {
        int a[2][2] = {{1,2},{3,4}};
    
        foo(a);
    }
    is this the only way foo would work
    Code:
    void foo( int array[][2] )
    {
      // code..
    }
    4. When a compiler processes the below code
    Code:
    int a[2][2] = {{1,2}, {3,4}};
    int b[4] = {1,2,3,4};
    does it store any additonal data for the 2d array ? Like i.e. row length. Atleast in memory they seem to store the data the same in the same order. Im just wondering how they are different.

    I'm interested in the difference between i.e. 1d array and a 2d array. So if anyone knows any good tutorials or articles please add the link.

    Thanks for any help.
    Last edited by laasunde; 08-14-2003 at 04:57 AM.

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    1
    Here Im not 100% sure but maybe someone else can confirm this. When sizeof() is used on an array the size of the whole array is returned (I think they implemented it this way so there is a chance to know how big the array is), and not the size of a pointer like in foo (the int array[] parameter is converted to a pointer).

    2
    Code:
    int* ptr = a;
    change it to
    Code:
    int (*ptr)[2] = a;
    More info about the subject here (they do a better job than me).

    3
    And the other way is the "exact " parameter
    Code:
    void foo( int array[2][2] )
    {
      // code..
    }
    This is possible because this is possible
    Code:
    int array1[] = {12, 45, 90};
    int array2[3] = {12, 45, 90};
    4
    This one im not sure about but I have 2 theories
    • Pointer to pointer arrays
    • Simulating nD arrays as 1D array
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things dont come easy in life!!!

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    2)

    int a[2][3];
    int* ptr = a;

    The leftmost dimension is not part of the type of an array. So, a's type is int[3]. A pointer to that array must be declared with that type, and this is the syntax:

    int (*a_pointer)[3];

    The parentheses are necessary, otherwise you would have this:

    int* a_pointer [3];

    and that creates an array of 3 int pointers, not one pointer to type int[3].


    4) Maybe this will help demonstrate some additional traits of pointers to arrays:
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int a[2][3] = {{1,2,3}, {4,5,6}};
    	int b[6] = {1,2,3,4,5,6};
    
    	cout<<**(a+1)<<endl;//jumps to the next row
    	cout<<*(b+1)<<endl;//jumps to the next element
    
    	//dynamically creating a 2d array:
    	int** parray = new int*[2];
    	parray[0]=new int[3];
    	parray[1]=new int[3];
    
    	//assigning values to the array:
    	*(parray[0] + 0) = 1;
    	*(parray[0] + 1) = 2;
    	*(parray[0] + 2) = 3;
    	
    	//display an element using array notation:
    	cout<<parray[0][2]<<endl;
    
    	delete [] parray[0];
    	delete [] parray[1];
    	delete [] parray;
    
    	return 0;
    }
    Last edited by 7stud; 08-14-2003 at 07:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. OpenGL drawing w/ Arrays or not?
    By tegwin in forum Game Programming
    Replies: 2
    Last Post: 01-14-2003, 03:31 PM
  3. arrays arrays arrays....
    By Jan79 in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2002, 07:16 AM
  4. Some C test questions: challenge
    By Mister C in forum C Programming
    Replies: 47
    Last Post: 09-10-2002, 07:47 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 08:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21