Thread: Exception in Assignment of 2D array

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Exception in Assignment of 2D array

    Following is the piece of code:

    I a have a function:
    Code:
    void q21_transpose(int **array,int rows,int columns)
    {
    	int temp,i,j;
    		
    	for(i=0;i<rows;i++)
    	{
    	
    		for(j=0;j<columns;j++)
    		{
    			temp = array[i][j]; //Exception is thrown here
    			
    			array[j][i] = array[i][j];
    			array[j][i] = temp;  	
    		}
    	}
    }
    Code calling the function:
    Code:
    int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}},**q21_result,i,j;
    q21_transpose(a,3,3);
    What is wrong with the above highlighted assignment statement?
    It throws exception at assignment

    Thanks in advance...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays and Pointers

    The name of an array is like a pointer to its first element's type. So you 'a' is a pointer to an array of 3 integers. That's different than a pointer to a pointer to an integer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    So how do I go about this problem.....
    Where in
    1. I have to return the 2D array and print them.
    2. How to accept argument of type 2D array ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the link I posted. You can't return arrays. You can return a pointer to something you've dynamically allocated, but you can't return an array.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you have int a[3][3] then the prototype and definition of the function can be

    void q21_transpose(int array[3][3],int rows,int columns)
    void q21_transpose(int array[][3],int rows,int columns)
    void q21_transpose(int (*array)[3],int rows,int columns)

    The first one is easy, as it's just a copy/paste of the declaration of the array you want to pass into the function.

    The columns parameter is redundant, as it can only ever be 3.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer assignment using array name
    By stavos77 in forum C Programming
    Replies: 7
    Last Post: 03-12-2010, 03:12 PM
  2. Need help with an array assignment
    By 3wit in forum C Programming
    Replies: 1
    Last Post: 04-17-2008, 10:52 AM
  3. Exception-Safe Copy Assignment
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 04-02-2008, 05:43 AM
  4. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 06:32 AM
  5. Operator for array assignment.
    By 39ster in forum C++ Programming
    Replies: 9
    Last Post: 01-06-2008, 11:22 PM