将指针(矩阵)传递给C中的函数 [英] Passing pointers (matrix) to a function in c

查看:116
本文介绍了将指针(矩阵)传递给C中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照通常的方式使用calloc动态创建了一个矩阵:

int **matrix;
int dim,r;
scanf("%d",&dim);
matrix=(int **)calloc(dim, sizeof(int *));
for(r=0; r<dim; r++)
   {
    matrix[r]=(int *)calloc(dim, sizeof(int));
   }

现在,如果我想创建一个函数以仅打印此矩阵的元素,我应该编写类似的内容:

void stampmatrix(int **matrix, int dim)
{
int r=0, c=0;
for(r=0; r<dim; r++)
    {
    printf("(");
    for(c=0;c<dim;c++)
        {
        printf(" %d , ",matrix[r][c]);
        }
    printf(")");
     }
}

这很好用. 现在,我将这一行添加到以前的功能中

`...`
 matrix[r][c]=1;
 printf(" %d , ",matrix[r][c]);
 ...

如果我在主函数中调用此函数, Stampmatrix(矩阵,昏暗) 一旦运行程序,编译器应创建矩阵的副本,将其填充为1,然后打印出来,然后返回到我的主要功能,而无需更改实际矩阵.但是,如果执行此操作,然后在我的主函数中检入矩阵元素的值,它们将更改为1. 在课堂上,我被告知,如果我将值传递给函数,则程序将创建值的副本,与它们一起使用,然后取消该副本,因此我需要将地址传递给函数,以实际更改我的内容.我的主要功能中的变量. 为什么在这种情况下它不起作用,并更改了我的矩阵值?这是因为我仍然将指针传递给函数stampmatrix吗?我真的不明白函数不应该是这样的:

void stampfunction(int dim, int ***matrix)

还是因为我使用了void函数? 感谢您的关注!

解决方案

请记住,在该程序中(我假设),所有代码共享相同的堆(内存),并且指针类似于该堆的路标. /p>

什么方法

void stampmatrix(int **matrix, int dim)

确实就像您给一个人一个数字,一个dim和一个物理图,例如将所有路标指向网格中的苹果一样.

是的,您按值 directions 传递给矩阵-您给了一个人苹果路标地图的副本.

您要做的另一件事是matrix[r][c]隐式取消了对指针的引用-或者,该方法说要遵循apple-signpost-map的方向一直指向该网格中的(r,c).

因此,当您说'matrix [r] [c] = 1'时,您要说的是确保离开时(r,c)上只有一个苹果.这就是为什么matrix [r] [c]上的值更改的原因.

I have dynamically created a matrix using calloc in the usual way:

int **matrix;
int dim,r;
scanf("%d",&dim);
matrix=(int **)calloc(dim, sizeof(int *));
for(r=0; r<dim; r++)
   {
    matrix[r]=(int *)calloc(dim, sizeof(int));
   }

Now if I wanted to create a function to just print the elements of this matrix, I should write something like:

void stampmatrix(int **matrix, int dim)
{
int r=0, c=0;
for(r=0; r<dim; r++)
    {
    printf("(");
    for(c=0;c<dim;c++)
        {
        printf(" %d , ",matrix[r][c]);
        }
    printf(")");
     }
}

And this works fine. Now I add this line to the previous function

`...`
 matrix[r][c]=1;
 printf(" %d , ",matrix[r][c]);
 ...

If i call this function in my main function, stampmatrix(matrix,dim) once i run the program, the compiler should create a copy of my matrix, fill it with 1, and then print them, and then return to my main function without changing the actual matrix. But if I do this and then i check in my main function the values of my matrix elements, they are changed to 1. In class i was told that if I pass values to a function, the program creates a copy of the values, works with them and then cancel the copy, so I need to pass addresses to a function in order to actually change the contents of my variables in my main function. Why in this case it doesn't work, and changes my matrix values? It's because I still pass pointers to the function stampmatrix? I really don't understand. Shouldn't the function be something like:

void stampfunction(int dim, int ***matrix)

Or it's because i used a void function? Thanks for the attention!

解决方案

Remember, in this program (I assume), all code shares the same heap (memory), and pointers are kind of like signposts, to that heap.

What the method

void stampmatrix(int **matrix, int dim)

does is like when you give a person a number, dim, and physical map to, say, all the signposts to apples in a grid.

So yes, you passed in by value directions to the matrix - you gave a person a copy of your apple-signpost-map.

The other thing you did is that matrix[r][c] implicitly dereferences the pointer - or, the method is saying to follow the apple-signpost-map directions all the way to point (r, c) in this grid.

So when you say `matrix[r][c] = 1', you're saying to make sure there's only one apple at (r, c) when you leave. That's why the value at matrix[r][c] changed.

这篇关于将指针(矩阵)传递给C中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆