Multidimensional arrays

This is a discussion on Multidimensional arrays within the C Programming forums, part of the General Programming Boards category; the way I see it, copying doesn't imply a change of types, and conversion does. also, linguistically, I'd guess that ...

  1. #31
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the way I see it, copying doesn't imply a change of types, and conversion does. also, linguistically, I'd guess that conversion suggests in-place conversion, but it doesn't really work like that in C... if you're only talking about temporary values, you might see it that way.

  2. #32
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Right, but then how is int->int a conversion?

  3. #33
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it's not, afaik.

  4. #34
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ok, so we're basically talking about specific wording in the standard, then - correct? If so, you'll have to forgive my ignorance - I'm a software engineer, not a language expert.

  5. #35
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I'm just going by my understanding... I'm no expert either... I'm wrong fairly regularly.

  6. #36
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well that clears it up.

  7. #37
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    lol I walked into that one.

  8. #38
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Hi

    I have a final question (I've said that quite a few times, but hopefully I will mean it this time!).

    We have

    Code:
    int array[2][2] { {1,2}, {3,4} };
    int (*ptr)[2];
    ptr = array;
    The address "array" is the address if the first array in "array[5][11]". How is this stored in memory?

    I mean, it is not like with string literals, since it is an initializer. So how does it work?

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The array here has nothing to do with either 5 or 11. You have space for four integers, and that space has been filled with the integers 1, 2, 3, and 4. (Just like with the previous two-dimensional array, the values are stored in order across and then down: [0][0], then [0][1], then [1][0], then [1][1].)

  10. #40
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Uf, the [5][11] was an error - it should of course have been [2][2].

    But how can an array have an address as a whole? It must have one, since it is passed to ptr.

  11. #41
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Niels_M View Post
    Uf, the [5][11] was an error - it should of course have been [2][2].

    But how can an array have an address as a whole? It must have one, since it is passed to ptr.
    Every object has an address, which is where it is stored. An array is no different -- it has four constituent objects, yes, but the array itself lives somewhere and that is its address. So array and array[0] and array[0][0] share the same address, because they all start at the same place.

  12. #42
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    If I use

    Code:
    	int asd[2][3]={{1,2,5},{3,4,6}};
    then what is the difference between asd+1 and *(asd+1)? The first is the address of the {3,4,5}-array, and *(asd+1) "accesses" the array or what?

  13. #43
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    When you declare an array of any dimension, the compiler sets aside a block of contiguous bytes, and uses the array name as a reference to the first element. So for example:

    Code:
    	size_t 
    		data[ 3 ][ 4 ];
    The compiler allocates 3 * 4 * sizeof( size_t ) bytes on the stack, and since the array name is an reference to the first element, then ( ( size_t* )data ) == &data[ 0 ][ 0 ]. And because we're dealing with an array of arrays, ( ( size_t* )( data + 1 ) ) == &data[ 1 ][ 0 ]. Maybe this will make it more clear:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
    	size_t 
    		data[ 3 ][ 4 ] = 
    		{ 
    			{ 1, 2, 3, 4 }, 
    			{ 5, 6, 7, 8 }, 
    			{ 9, 10, 11, 12 }
    		}, 
    		size_in_bytes = sizeof( data ), 
    		number_of_elements = size_in_bytes / sizeof( size_t ), 
    		rows = size_in_bytes / sizeof( data[ 0 ] ), 
    		columns = number_of_elements / rows, 
    		row, 
    		column;		
    	printf( "size_in_bytes: %d \n", size_in_bytes );
    	printf( "number_of_elements: %d \n", number_of_elements );
    	printf( "rows: %d \n", rows );
    	printf( "columns: %d \n", columns );
    	printf( "address of data: %x \n", ( size_t* )data );
    	printf( "address of data + 1: %x \n", ( size_t* )( data + 1 ) );
    	for( row = 0; row < rows; ++row )
    	{
    		for( column = 0; column < columns; ++column )
    		{
    			printf
    			( 
    				"<%x> data[ %d ][ %d ] = %d \n", 
    				&data[ row ][ column ], 
    				row, 
    				column, 
    				data[ row ][ column ] 
    			);
    		}
    	}
    	return 0;
    }

  14. #44
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    I am still unclear about it. I am used to that data+1 is the address of element #2, and that *(data+1) is the value.

    In this case, *(data+1) is the same address as data+1. I cannot see why.

  15. #45
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Niels_M View Post
    I am still unclear about it. I am used to that data+1 is the address of element #2, and that *(data+1) is the value.

    In this case, *(data+1) is the same address as data+1. I cannot see why.
    Right, well this is a two-dimensional array, so the second 'element' is an array, namely the one that starts at &data[ 1 ][ 0 ]. So then ( data + 2 ) == &data[ 2 ][ 0 ], and so on.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 02:58 PM

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