I am studying CSAPP and the book says we can use this following code :
#include <cstddef>
int var_ele(size_t n, int A[n][n], size_t i, size_t j) {
return A[i][j];
}
but I cannot compile this. The book says in the past we need to write like this :
#define IDX(n, i, j) ((i)*(n)+(j))
/* Get element A[i][j] */
int vec_ele(size_t n, int *A, size_t i, size_t j){
return A[IDX(n,i,j)];
}
I use command :
gcc -S -Og nn_matrix_code.cpp
on Ubuntu and the compiler says :
nn_matrix_code.cpp:2:30: error: use of parameter outside function body before ‘]’ token
2 | int var_ele(size_t n, int A[n][n], size_t i, size_t j) {
| ^
nn_matrix_code.cpp:2:33: error: use of parameter outside function body before ‘]’ token
2 | int var_ele(size_t n, int A[n][n], size_t i, size_t j) {
| ^
nn_matrix_code.cpp:2:34: error: expected ‘)’ before ‘,’ token
2 | int var_ele(size_t n, int A[n][n], size_t i, size_t j) {
| ~ ^
| )
nn_matrix_code.cpp:2:43: error: expected initializer before ‘i’
2 | int var_ele(size_t n, int A[n][n], size_t i, size_t j) {
| ^
Can anybody tell me what is wrong?