Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam trying to do make a program to swap a 2d array using function reference and when writing it i tell me incomplete type not used

C++
#include <stdio.h>
#include<conio.h>

    void swap(int *x[][2],int x1,int y1,int x2,int y2)
    {
	int temp=*x[x1][y1];
	*x[x1][y1]=*x[x2][y2];
	*x[x2][y2]=temp;
    }

    int main (void)
    {
	int arr[2][2];
	printf("Enter the array ");
	for (int i=0;i<2;i++)
	{
		for(int j=0;j<2;j++)
		{
			scanf("%d",&arr[i][j]);
		}
	}
	for(int i=0;i<2;i++)
	{	
		for(int j=0;j<2;j++)
		{
			printf("%d",&swap(arr,0,0,0,0));
		}
	}
	getch();
    }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 28-Dec-14 20:22pm
v2
Comments
chandanadhikari 29-Dec-14 2:53am    
on a side note :
printf("%d",&swap(arr,0,0,0,0)); //might be a logical mistake
the arguments should not be all 0 as int x1,int y1,int x2,int y2 all are having same value 0, so the swap will actually not happen

1 solution

You don't need to pass a pointer to an array to in-place change its elements. For instance, the following code swaps the first with the last item of the 2x2 array:

C
#include <stdio.h>

void myswap( int a[][2], int x1, int y1, int x2, int y2)
{
  int temp = a[x1][y1];
  a[x1][y1] =  a[x2][y2];
  a[x2][y2]= temp;
}


void dump(int a[][2], int n)
{
  int i, j;
  for (i=0; i<n; ++i)
  {
    for (j=0; j<2; ++j)
      printf("%d ", a[i][j]);
    printf("\n");
  }
}


int main (void)
{
  int arr[2][2];
  int i,j;
  printf("Enter the array ");

  for (i=0;i<2;i++)
  {
    for(j=0;j<2;j++)
    {
      scanf("%d",&arr[i][j]);
    }
  }
  dump(arr, 2);
  myswap(arr, 0, 0, 1, 1);
  dump(arr, 2);
  return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900