0

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; 
6
Contributors
10
Replies
69
Views
2 Years
Discussion Span
Last Post by Ancient Dragon
Featured Replies
  • 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! Read More

0

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 by COKEDUDE

0

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 by sepp2k

1

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!

0

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 by deceptikon

0

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;
1

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

Edited by sepp2k

Votes + Comments
yes :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.