Can you please explain what this weird int declaration does. Here is the block of code.

void show(void *u, int w, int h)
{
    int (*univ)[w] = u;
    printf("\033[H");
    for_y {
    for_x printf(univ[y][x] ? "\033[07m  \033[m" : "  ");
    printf("\033[E");
    }
    fflush(stdout);
}

This is the line I'm talking about.

int (*univ)[w] = u; 

They have #defines at the top.

#define for_x for (int x = 0; x < w; x++)
#define for_y for (int y = 0; y < h; y++)
#define for_xy for_x for_y

The initialization part also looks different than what I am used to. I am used to this with int.

int point[6]={0,0,1,0,0,0};

And this with char.

char string[] = "Merkkijono";
char string[] = {'M', 'e', 'r', 'k', 'k', 'i', 'j', 'o', 'n', 'o', '\0'};

Edited 2 Years Ago by COKEDUDE

VLAs are perfectly valid in C99 and "GNU89" - the only reason it wouldn't work with gcc/MinGW is if you explicitly set the standard to C89. And it doesn't work in VS because Visual Studio's C support is completely outdated.

Oh and I should mention that this question has also been asked and answered here, so there's no need to repeat that here.

Edited 2 Years Ago by sepp2k

Those macros are obscene and should never be used in code that anyone else ever has to work with. They may make the code shorter but they obscure its meaning. They also use variables that are not passed to the macro. Blecch!

And it doesn't work in VS because Visual Studio's C support is completely outdated.

So are standard VLAs. ;) You'll notice that C11 made them optional for the implementation. Be sure to check __STD_NO_VLA__ to see if you're SOL. Even if they're available, you have some hoops to jump through to provide even remote reliability since the most common implementation (all of them that I'm aware of) diddles with the stack.

VLAs are a great idea in theory. In practice, they've been less than stellar. Sadly, the standard VLA model is no better than alloca. So I typically recommend against using them in favor of a more traditional dynamic array using malloc.

Edited 2 Years Ago by deceptikon

I have gcc version 4.8.1, (c) 2013 and may be outdated. It was installed with Code::Blocks version 13.12 just the other day. Here are the errors.

mingw32-g++.exe -Wall -fexceptions -g -std=c++11 -Wall  -c C:\dvlp\Test\main.cpp -o obj\Debug\main.o
C:\dvlp\Test\main.cpp: In function 'void show(void*, int, int)':
C:\dvlp\Test\main.cpp:3:21: error: invalid conversion from 'void*' to 'int (*)[(((sizetype)(((ssizetype)w) + -1)) + 1)]' [-fpermissive]
     int(*univ)[w] = u;
                     ^
C:\dvlp\Test\main.cpp:3:10: warning: unused variable 'univ' [-Wunused-variable]
     int(*univ)[w] = u;

Yes, VLAs and implicit casts from void are not allowed in C++. You'll have to compile this as C code.

Edited 2 Years Ago by sepp2k

Comments
yes :)
This article has been dead for over six months. Start a new discussion instead.