简体   繁体   English

我可以将int传递给C中的void函数吗?

[英]Can I pass an int to a void function in C?

I'm trying to use functions to let the user enter a n by n square matrix and print it out. 我正在尝试使用函数让用户输入n x n方阵并将其打印出来。

The function for entering a matrix is: ucitajmatricu 输入矩阵的函数是: ucitajmatricu

And to print it out: ispisimatricu 并打印出来: ispisimatricu

#include < stdio.h > 
#include < stdlib.h >

void ucitajmatricu(int);
void ispisimatricu(int);

int main() {
  int n;
  scanf("%d", & n);

  ucitajmatricu(n);
  ispisimatricu(n);

  return 0;
}

void ucitajmatricu(int n) {
  int i, j;

  int MAT[n][n];

  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      scanf("%d", & MAT[i][j]);
    }
  }
  printf("\n");

  return;
}

void ispisimatricu(int n) {
  int i, j, MAT[n][n];
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      printf("%d\t", MAT[i][j]);
    }
    printf("\n");
  }

  return;
}

Example input: 3 输入示例: 3

1 2 3
4 5 6
7 8 9

Example output: 输出示例:

1 2 3
4 5 6
7 8 9

Actual output: 实际输出:

1 2 3
4 5 6
7 48 0

void ucitajmatricu(int n) { void ucitajmatricu(int n){
... ...
int MAT[n][n]; int MAT [n] [n];

... ...

void ispisimatricu(int n) { void ispisimatricu(int n){
int i, j, MAT[n][n]; int i,j,MAT [n] [n];

The two declarations of MAT[][] are in different functions - they do not retain their value and the fact they have the same name has no importance. MAT [] []的两个声明具有不同的功能-它们不保留其值,并且它们具有相同的名称并不重要。

Declare MAT[][] once, outside a function. 在函数外声明一次MAT [] []。 And, BTW, this problem seems to have nothing to do with the title of the question. 而且,顺便说一句,这个问题似乎与问题的标题无关。

EDIT after comment 评论后编辑

The program seems to work-only-partly because in the first call the values are read and stored in a local array (in the stack). 该程序似乎只能工作,部分原因是在第一个调用中读取了值并将其存储在本地数组(在堆栈中)中。 The second called function has a similar (if not identical) stack frame, so the frame declared and instantiated in the second call is overlaid to the previous one, by chance, almost 100%. 第二个被调用函数具有相似(如果不相同)的堆栈框架,因此在第二个调用中声明和实例化的框架几乎覆盖了前一个框架,即100%。 The second function appears to mantain a large part of the values, but this is just luck (or unluck...). 第二个函数似乎保留了大部分值,但这只是运气(或运气...)。 If, between the two calls, there were other calls to other functions, the stack would get more corrupted (well, overwritten) and the effect of work-only-partly would decrease. 如果在这两次调用之间还有其他对其他函数的调用,则堆栈将更容易损坏(很好,被覆盖),并且部分工作的效果将降低。

The type of bug in the OP program is nasty because the stack "luckily" mantains part of the values, but "unluckily" this fact masks out the real problem: local variables disappear after the function exits. OP程序中的错误类型令人讨厌,因为堆栈“幸运地”保留了部分值,但“不幸地”,这一事实掩盖了真正的问题:局部变量在函数退出后消失了。

To elaborate on the above answer, when you declare a variable in a function you are declaring it on the stack. 为了详细说明以上答案,当您在函数中声明变量时,即在堆栈上声明它。

You could declare and initialize globally but that would be different than what it appears you want to do. 您可以全局声明和初始化,但这与您想要执行的操作不同。 Easier in some ways since depending on stack memory but more rigid. 在某些方面更容易,因为取决于堆栈内存,但更严格。

To use your current implementation initialize your matrix by allocating on the heap instead. 要使用当前的实现,请改为通过在堆上分配来初始化矩阵。 Use calloc() to allocate and zero the space. 使用calloc()分配空间并将其清零。 Then when you return, return the pointer to your matrix. 然后,当您返回时,将指针返回到矩阵。 Downside is more memory management required. 缺点是需要更多的内存管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM