简体   繁体   English

我不确定为什么该程序无法编译

[英]I'm not sure why this program won't compile

Here is my program: 这是我的程序:

#include<stdio.h>
#define COL 3;

void copy_row(int arr1[][COL], int rows, int arr2[], int r);

void copy_row(int arr1[][COL], int rows, int arr2[], int r){
  int i;
  if(r >= rows || r < 0)
    return;

  for(i = 0; i < COL; i++){
    arr1[r][i] = &arr2[i];
  }
}

int main(void){

  return 0;
}

When I try to compile this with gcc, it says "error: expected ']' before ';' 当我尝试使用gcc进行编译时,它会在“;”之前说“错误:期望的']'。 token" on line 4 and line 6. 令牌”位于第4行和第6行。

Also, refer to the for-loop, would "arr1[r][i] = arr2[i];" 另外,请参阅for循环,将“ arr1 [r] [i] = arr2 [i];” do the samething as "arr1[r][i] = &arr2[i];"? 与“ arr1 [r] [i] =&arr2 [i];”相同吗? Which is is (more) correct? 哪个是(更多)正确的?

The macro COL is expanded to 3; COL扩展为3; , so inside your declaration and definition looks like this: ,因此您的声明和定义中的内容如下所示:

int arr1[][3;]

Remove the semi-colon from your #define line. #define行中删除分号。

Also, refer to the for-loop, would arr1[r][i] = arr2[i]; 同样,参考for循环, arr1[r][i] = arr2[i]; do the same thing as arr1[r][i] = &arr2[i]; 做与arr1[r][i] = &arr2[i]; ?

No. The first one is correct. 不,第一个是正确的。 The lvalue arr1[r][i] refers to an integer. 左值 arr1[r][i]是整数。 The rvalue arr2[i] is also an integer. 右值 arr2[i]也是一个整数。

If you reference it by taking &arr2[i] then its type becomes int* , and is a pointer to element i , and you try to assign that to an lvalue of type int . 如果使用&arr2[i]引用它,则其类型变为int* ,并且是指向元素i的指针,然后尝试将其分配给int类型的左值 If you try to do that, your compiler should warn you. 如果尝试这样做,则编译器应发出警告。

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

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