0

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
dubugger
  • 89
  • 6
  • The problem could be if you're trying to compile that as C++. ISO C supports VLAs (variable-length arrays), C++ doesn't (except as an extension in some compilers; e.g. GCC but not MSVC). You tagged this [compiler-errors], but you didn't actually show any or mention any compilers. – Peter Cordes Feb 12 '22 at 02:33
  • I've modified my question. – dubugger Feb 12 '22 at 02:36
  • GCC supports VLAs in C++ mode (https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html), but apparently only inside functions, not for this use of function args. (Unless that's a compiler bug? But all GCC versions on Godbolt seem the same. https://godbolt.org/z/nT3TvPxbh). Clang accepts it in C++ mode. Anyway, this is not valid ISO C++, but is valid C. GCC compiles it just fine as C: https://godbolt.org/z/TPKc9f4qq – Peter Cordes Feb 12 '22 at 02:42
  • Oh, thank you very much. I change it into C and it can compile. I once thought C++ should compile all C code. – dubugger Feb 12 '22 at 02:44
  • 1
    hahaha :P Try `int *p = malloc(16);` for another example. (Implicit conversion from `void*`) – Peter Cordes Feb 12 '22 at 02:46
  • Does the book really say to write exactly what you have written in your first code block? That is neither valid C nor C++. Which of the two languages is the book intending to teach? – user17732522 Feb 12 '22 at 02:46
  • Don't use `gcc` to compile C++ programs. Use `g++` - otherwise you'll get problems when linking later. If you change to C, then include `stddef.h` instead of `cstddef` – Ted Lyngmo Feb 12 '22 at 02:54
  • Your code uses variable-length arrays (array with length determined at run time). VLA's are a feature of C. VLAs are NOT part of standard C++. Unfortunately, some C++ compilers (but certainly not all) partially support VLAs as a NON STANDARD extension (and your particular manner of usage is often the part that is not supported even by C++ compilers that do support VLAs). If your textbook is telling you that such things are standard C++, get another textbook. [BTW: VLAs are often considered poor practice in C, too]. – Peter Feb 12 '22 at 08:17

0 Answers0