2

I've read multiple posts about this issue and have not been able to solve my problem. I have a file with a .c extension that compiles perfectly on gcc. I've made sure that in the project properties it is set to Compile to C code. Still, when I try to compile, it fails because it won't allow this me to declare an array like this:

int scArray[N][v];

This indicates to me that it is trying to compile my code as C++ (maybe I'm wrong on that though).

What can I do to fix this?

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
mh234
  • 37
  • 6
  • It is a C++ compiler, so no wonder. Visual Studio is pure crap when it comes to compiling C. Use a C compiler instead. – Lundin Sep 30 '14 at 06:14
  • @Lundin: Which compiler one uses is not always their choice. I agree in general though; if you have the option, chose a C compiler which cares about supporting C. – Ed S. Sep 30 '14 at 06:24
  • You know, typically one selects an answer as accepted when they, you know, answer your question. You have never chosen an answer on here. Consider it. I wonder why they took that percentage out of the UI. – Ed S. Oct 01 '14 at 05:35

1 Answers1

3

VS2013 did add a fair amount of C99 library support, but it does not support VLA's as far as I am aware. At least, I could not find a mention of it in any MS article and the code doesn't compile, so... I'm assuming that it does not. See this article for library support.

EDIT: Per your comment, you have to allocate the memory dynamically.

To allocate your pointer:

int **p = malloc(N * sizeof *p); 
for(int i = 0; i < N; ++i) 
    p[i] = malloc(v * sizeof(int));

If your array dimensions were known at compile time, this would allocate one big block instead of performing N + 1 allocations:

int (*p)[COLS] = malloc(sizeof *arr * ROWS);

Of course, you could also just allocate a big chunk and do the math yourself, but you lose the p[x][y] syntax:

int *p = malloc(N * v * sizeof(int));
// fill with data
int elem = p[row * width + col];
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • See [C++11/14 Feature Tables For Visual Studio 14 CTP1](http://blogs.msdn.com/b/vcblog/archive/2014/06/11/c-11-14-feature-tables-for-visual-studio-14-ctp1.aspx) and [CTP3](http://blogs.msdn.com/b/vcblog/archive/2014/08/21/c-11-14-features-in-visual-studio-14-ctp3.aspx) for what parts of C99 are in progress for Visual Studio "14". Historically, [C99](http://blogs.msdn.com/b/vcblog/archive/2007/11/05/iso-c-standard-update.aspx) support has never been a high priority for the Visual C++ team, but since C++11 includes C99 as part of the standard it is getting more support. – Chuck Walbourn Sep 30 '14 at 05:48
  • @ChuckWalbourn: Yeah, I found those. No mention of VLA's, so I think it's safe to assume it's not in the works. – Ed S. Sep 30 '14 at 05:50
  • So what is an alternative if I need to declare an array of size determined by user input? I'm new to C and Visual Studio, so I'm not quite sure how to approach this. – mh234 Sep 30 '14 at 05:51
  • You allocate it on the heap with ``malloc``, you don't try to allocate it on the stack which is how you'd do it with C89. That or just use C++ and ``std::vector``. – Chuck Walbourn Sep 30 '14 at 05:55