0

Given code is for generating a magic square in which i have used VLA for the function

create_magic_square(int n, int magic_square[n][n])
print_magic_square(int n, int magic_square[n][n])

 #include <stdio.h>

void create_magic_square(int n, int magic_square[n][n]);
void print_magic_square(int n, int magic_square[n][n]);

int main()
{
    int size;
    printf("This program creates a magic square of a specified size");
    printf("The size be an odd number between 1 and 99");

    printf("Enter the size of magic square: ");
    scanf("%d", &size);
    if(size%2 == 0 || size < 0 || size > 99)
    {
        printf("Wrong Entry!!!");
        return 0;
    }

    int square[size][size];
    for( int i = 0; i < size; i++)
        for(int j = 0; j < size; j++)
            square[i][j] = 0;

    create_magic_square(size, square);
    print_magic_square(size, square);

    return 0;   
}

void create_magic_square(int n, int magic_square[n][n])
{
    int row = 0, col = n/2;
    magic_square[row][col] = 1;
    while(magic_square[row][col] <= n*n)
   {
        int new_row = ((row - 1) + n) % n;
        int new_col = ((col + 1) + n) % n;
        if(magic_square[new_row][new_col] == 0)
        {
            magic_square[new_row][new_col] = magic_square[row][col] + 1;
            row = new_row;
            col = new_col;
        }
        else if(magic_square[new_row][new_col] != 0)
        {
            magic_square[row + 1][col] = magic_square[row][col] + 1;
            row = row + 1;
        }
    }
}

void print_magic_square(int n, int magic_square[n][n])
{
    for( int i = 0; i < n; i++)
   {
        for(int j = 0; j < n; j++)
           printf("%d   ", magic_square[i][j]);
        printf("\n\n"); 
    }   

}

when file saved with extension .cpp ,On compiling it is giving the following error:

enter image description here

When I changed this extension to .c, it worked fine.
What is the reason behind this?
I think VLAs are not allowed in C++, is it right?

NOTE: Check this link regarding VLAs as parameter:
Why use an asterisk "[*]" instead of an integer for a VLA array parameter of a function?

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264

1 Answers1

5

You can't use C-style array that way, all but the first extent must be compile-time constant.

What you can do is pass int *magic_square that points to a one-dimensional n*n array, and use a simple index-mapping function to get the linear index of the cell.

You tagged the question as C++, so you should know that

int square[size][size];

is not valid C++ either, though it's valid C99 and some compilers support it through extension.

For C++ I'd suggest to use std::vector<int> vec(size*size) as holder.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
Balog Pal
  • 16,195
  • 2
  • 23
  • 37
  • I don't have any idea about C++? – haccks Jul 02 '13 at 11:12
  • @BalogPal: I've just stumbled upon the same thing. Actually, in C it seems to be able to VLA in parameters. Haccks provided an very interesting link, please see: http://stackoverflow.com/questions/17371645/parameter-of-a-function – quetzalcoatl Jul 02 '13 at 13:35
  • @haccks: btw, thank you for the link! I've never see the `[*]` before, yet I'm even more suprised to be allowed to use VLA as parameters. I've removed my answer as all that would be left after corrections would relate to C++ not C. – quetzalcoatl Jul 02 '13 at 13:38
  • @quetzalcoatl: the linked article uses 1-dimensional array, where the number in [] is ignored anyway. but the [*] is an interesting extension – Balog Pal Jul 02 '13 at 13:38
  • Sorry, wrong link. I meant [the answer](http://stackoverflow.com/a/17371914/717732) not the question. In the answer the writer provides a prototype of `int sum2d(int n, int m, int a[n][m]);` which certainly is 2D. I actually NOT have a compiler at hand to check it. Just thought you may want to skim through it. – quetzalcoatl Jul 02 '13 at 13:41
  • @quetzalcoatl: thinking again, it's really not hard to implement that way in the extensions in the function body, only the declaration syntax needs some fiddling – Balog Pal Jul 02 '13 at 13:44
  • 1
    @quetzalcoatl: Your welcome. I have checked all the prototypes explained by answer writer on the provided link, all are working. – haccks Jul 02 '13 at 13:49