即使按值传递,多维数组值也会被修改
Multidimensional array values getting modified even though passed by value
我已将二维数组正常(按值)传递给函数 "elem",函数 "elem" 进一步将其传递给另一个函数 "interchange",该函数执行行交换操作并显示它。但问题是,在我 return 从交换返回 main() 之后,数组的值已更改为交换的结果数组,即使从技术上讲它们必须是三个不同的变量用于三个不同的函数(main , 元素和互换)。为什么会这样,如何让main()中的数组保持不变?
//include files...
void interchange(float c[10][10],int m,int n)
{
int i,j,p,q;
float temp;
printf("\nEnter the two row numbers to interchange:");
scanf("%d%d",&p,&q);
if((--p<m)&&(--q<n))
{
for(i=0;i<m;i++)
{
temp=c[p][i];
c[p][i]=c[q][i];
c[q][i]=temp;
}
} else
{
printf("Row numbers must be less than matrix order.\n");
return;
}
printf("\nResultant matrix is:\n"); //print the array in interchange,c
printf("\n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{
printf("%f\t",c[i][j]);
}
printf("\n");
}
}
void elem(float b[10][10],int m,int n)
{
int ch;
do
{
printf("\n1:Row interchange\t 2:Exit elementary transformations\n\nEnter the choice:");
scanf("%d",&ch); //get user input to decide which operation to perform (there are more operations in actual code)
switch(ch)
{
case 1: interchange(b,m,n);
break;
case 2: printf("\nExiting elementary transformations.\n");
break;
}
}while(ch!=2);
}
int main()
{
float a[10][10];
int m,n,i,j;
printf("Enter the order of the matrix:");
scanf("%d%d",&m,&n);
printf("Enter the matrix elements:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&a[i][j]);
}
}
//print the entered array a
printf("\n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
elem(a,m,n);
//print the original array in main()
printf("\n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
}
这是我得到的输出:
Enter the order of the matrix:2 2
Enter the matrix elements:1 2 3 4
1.000000 2.000000
3.000000 4.000000
1:Row interchange 2:Exit elementary transformations
Enter the choice:1
Enter the two row numbers to interchange:1 2
Resultant matrix is:
3.000000 4.000000
1.000000 2.000000
1:Row interchange 2:Exit elementary transformations
Enter the choice:2
Exiting elementary transformations.
3.000000 4.000000
1.000000 2.000000
抱歉,代码很糟糕,我从一个更大的文件中抢救了它。
Why is this so?
是的,您正在传递它 "by value",但您必须了解这在 C 中的含义。当您将数组传递给函数时,它会 衰减到一个指针。然后该指针按值传递,但该值是数组中第一个元素的地址。
and what can I do to make the array in main() remain unchanged?
一些不同的东西,取决于你想要什么。您可以在函数中创建一个临时数组,然后复制所有值。将开头替换为:
void interchange(const float arr[10][10],int m,int n) {
float c[10][10];
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
c[i][j]=arr[i][j];
如果没有其他错误,您的代码将按您希望的方式工作。添加 const
的效果是,如果您尝试将 arr
中的元素分配给某物,则会出现编译器错误。
对于函数参数,您不能传递数组"by value"。
当编译器看到参数声明 float c[10][10]
时,它会将其翻译为 float (*c)[10]
。
所以传递的是一个指针(指向数组的第一个元素)。您修改指针指向的内存,这会反映在调用函数中。
我已将二维数组正常(按值)传递给函数 "elem",函数 "elem" 进一步将其传递给另一个函数 "interchange",该函数执行行交换操作并显示它。但问题是,在我 return 从交换返回 main() 之后,数组的值已更改为交换的结果数组,即使从技术上讲它们必须是三个不同的变量用于三个不同的函数(main , 元素和互换)。为什么会这样,如何让main()中的数组保持不变?
//include files...
void interchange(float c[10][10],int m,int n)
{
int i,j,p,q;
float temp;
printf("\nEnter the two row numbers to interchange:");
scanf("%d%d",&p,&q);
if((--p<m)&&(--q<n))
{
for(i=0;i<m;i++)
{
temp=c[p][i];
c[p][i]=c[q][i];
c[q][i]=temp;
}
} else
{
printf("Row numbers must be less than matrix order.\n");
return;
}
printf("\nResultant matrix is:\n"); //print the array in interchange,c
printf("\n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{
printf("%f\t",c[i][j]);
}
printf("\n");
}
}
void elem(float b[10][10],int m,int n)
{
int ch;
do
{
printf("\n1:Row interchange\t 2:Exit elementary transformations\n\nEnter the choice:");
scanf("%d",&ch); //get user input to decide which operation to perform (there are more operations in actual code)
switch(ch)
{
case 1: interchange(b,m,n);
break;
case 2: printf("\nExiting elementary transformations.\n");
break;
}
}while(ch!=2);
}
int main()
{
float a[10][10];
int m,n,i,j;
printf("Enter the order of the matrix:");
scanf("%d%d",&m,&n);
printf("Enter the matrix elements:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&a[i][j]);
}
}
//print the entered array a
printf("\n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
elem(a,m,n);
//print the original array in main()
printf("\n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
}
这是我得到的输出:
Enter the order of the matrix:2 2
Enter the matrix elements:1 2 3 4
1.000000 2.000000
3.000000 4.000000
1:Row interchange 2:Exit elementary transformations
Enter the choice:1
Enter the two row numbers to interchange:1 2
Resultant matrix is:
3.000000 4.000000
1.000000 2.000000
1:Row interchange 2:Exit elementary transformations
Enter the choice:2
Exiting elementary transformations.
3.000000 4.000000
1.000000 2.000000
抱歉,代码很糟糕,我从一个更大的文件中抢救了它。
Why is this so?
是的,您正在传递它 "by value",但您必须了解这在 C 中的含义。当您将数组传递给函数时,它会 衰减到一个指针。然后该指针按值传递,但该值是数组中第一个元素的地址。
and what can I do to make the array in main() remain unchanged?
一些不同的东西,取决于你想要什么。您可以在函数中创建一个临时数组,然后复制所有值。将开头替换为:
void interchange(const float arr[10][10],int m,int n) {
float c[10][10];
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
c[i][j]=arr[i][j];
如果没有其他错误,您的代码将按您希望的方式工作。添加 const
的效果是,如果您尝试将 arr
中的元素分配给某物,则会出现编译器错误。
对于函数参数,您不能传递数组"by value"。
当编译器看到参数声明 float c[10][10]
时,它会将其翻译为 float (*c)[10]
。
所以传递的是一个指针(指向数组的第一个元素)。您修改指针指向的内存,这会反映在调用函数中。