1

learning the C programming language and reading the documentation with Xcode 13.2 open and a Command-Line Tool project in front of me.
Reading this, in the Declarations/Arrays/Variable Length Arrays section:

{
   int n = 1;
label:
   int a[n]; // re-allocated 10 times, each with a different size
   printf("The array has %zu elements\n", sizeof a / sizeof *a);
   if (n++ < 10) goto label; // leaving the scope of a VLA ends its lifetime
}

And copying it in Xcode, inside the main function, it just gives me the "Expected expression" error next to the int a[n]; line. I tried to put this into a separate function but this was not the solution.
What is going wrong here?
Thank you

NotationMaster
  • 390
  • 3
  • 17

1 Answers1

4

The only thing that can follow a label is a statement, and a declaration is not a statement. You'll have to wrap the code following the label in a block somehow:

#include <stdio.h>

int main( void )
{
  int n = 1;
label:
   do {
    int a[n];

    printf( "The array has %zu elements\n", sizeof a / sizeof a[0] );
    if ( ++n < 10 ) goto label;
  } while ( 0 );

  return 0;
}

Now the results should be what you expect:

$ ./vla
The array has 1 elements
The array has 2 elements
The array has 3 elements
The array has 4 elements
The array has 5 elements
The array has 6 elements
The array has 7 elements
The array has 8 elements
The array has 9 elements

For the love of God don't do this.

EDIT

Using just an empty statement after the label:

#include <stdio.h>

int main( void )
{
  int n = 1;
label:
  ;
  int a[n];

  printf( "The array has %zu elements\n", sizeof a / sizeof a[0] );
  if ( ++n < 10 ) goto label;    
  return 0;
}
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • 2
    Or just use `;` the empty statement. – o11c Jan 18 '22 at 19:20
  • @o11c: Or that, yeah - working on another issue for `$DAYJOB` and not spending a lot of cycles on this – John Bode Jan 18 '22 at 19:22
  • @o11c what is the `;` empty statement and how would I use it in this case? – NotationMaster Jan 18 '22 at 19:27
  • 1
    @NotationMaster: just put a `;` on a line between the label and the array declaration - C99 and later support mixed declarations and code. You just need a statement (which includes an empty statement) after the label to make it work. – John Bode Jan 18 '22 at 19:29
  • @JohnBode thank you, I will never do this conscientiously, I am just studying :-) – NotationMaster Jan 18 '22 at 19:30
  • @NotationMaster personally I tend to put the semicolon on the same line, but yeah, that. Also note that, even though MSVC (the worst C compiler still in use) doesn't support all of C99, "mixed declarations and code" is one of the parts it does support, so there is no reason to put all the declarations up front like ancient compilers required. (MSVC also does not support VLAs, but VLAs should be avoided even on compilers that do support them). – o11c Jan 18 '22 at 19:35
  • I am a very beginning, not even a programmer yet, just passionate about it and learning it using the tools I have and hoping they do what is necessary. Is by any change that MSVC == Xcode returns true? :) – NotationMaster Jan 18 '22 at 19:37
  • 1
    As far as the *compiler itself* is concerned, XCode >>>>> MSVC (XCode should support the most recent language standard, MSVC does not). As far as the other IDE features are concerned (editor, project manager, debugger, etc.), I have no opinion. I don't use either in may day-to-day work. – John Bode Jan 18 '22 at 19:41