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 ';' token" on line 4 and line 6.
Also, refer to the for-loop, would "arr1[r][i] = arr2[i];" do the samething as "arr1[r][i] = &arr2[i];"? Which is is (more) correct?
The macro COL
is expanded to 3;
, so inside your declaration and definition looks like this:
int arr1[][3;]
Remove the semi-colon from your #define
line.
Also, refer to the for-loop, would
arr1[r][i] = arr2[i];
do the same thing asarr1[r][i] = &arr2[i];
?
No. The first one is correct. The lvalue arr1[r][i]
refers to an integer. The rvalue arr2[i]
is also an integer.
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
. If you try to do that, your compiler should warn you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.