#Array!

52 messages · Page 1 of 1 (latest)

stoic gust
#

It gives me an error for variable-sized object may not be initialized except with an empty initializer: int mxn[row][column] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

#include <stdio.h>

void transpose(int a[][3], int b[][3], int row, int column) {

}

int main() {
    int row = 3;
    int column = 3;

    int mxn[row][column] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int nxm[row][column];

    transpose(mxn, nxm, row, column);

    return 0;
}
still apexBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

pearl edge
#

row and column are not considered constant expressions so you're creating a variable size array

#

not an array that necessarily changes size, just one that's size isn't known until runtime

#

But 3 is known before runtime

#

The problem is since the compiler thinks its size isn't known until runtime it doesn't want you trying to initialize it with an initializer which assumes a certain width and height

#

since... they might be wrong

#

But 3 is constant

#

So you can either replace occurances of row and column with 3 every time or you can make row and column const, which makes them constant expressions in this case, so its no longer a variable size array, its a fixed size array of size 3

#

Or you could use the preprocessor to achieve a similar thing to replaceing row and column with 3

#

There are many solutions

stone vapor
#

making them const is not the same thing as making them constant expressions

#

consider const int row = rand() % 3 + 1

old hawk
#

;compile

const int x = 4;
printf("const x = %d\n", x);
int *ptr = (int *) &x;
*ptr = 5;
printf("const x = %d\n", x);
flat gardenBOT
#
Program Output
const x = 4
const x = 5
flat gardenBOT
#
Program Output
const x = 4
const x = 4
old hawk
#

oh, it's UB?

delicate rock
#

Definitely

pearl edge
stone vapor
#

it's just a non-modifiable lvalue, like an array

pearl edge
#

;compile

const int x = 4;
int arr[x] = {1, 2, 3, 4};
flat gardenBOT
#
Compiler Output
<source>: In function 'main':
<source>:5:14: error: variable-sized object may not be initialized except with an empty initializer
    5 | int arr[x] = {1, 2, 3, 4};
      |              ^
Build failed
pearl edge
#

huj yeah

#

wtf I swear Ive seen this done before

#

wait is it true with globals

old hawk
pearl edge
#

Obviously I know const is not the same as constant expressions lol

delicate rock
#

Jacob Sorber made a video on this exact discrepancy:
https://youtu.be/8a3HyL1VN0Q?si=hd6YvIPNNiMzsrkJ

Patreon ➤ https://www.patreon.com/jacobsorber
Courses ➤ https://jacobsorber.thinkific.com
Website ➤ https://www.jacobsorber.com

A const int is not a constant // seriously. It's one of the oddest things about C, but it's true. In this video, I'll show you a few examples.


Welcome! I post videos that help you learn to program and becom...

▶ Play video
pearl edge
#

I could swear Ive seen someone use const in a weird way in c to somehow make a constant expression

#

maybe im going insane

delicate rock
old hawk
#

;compile

static const int x = 4;
int main() {
    int xs[x] = {1, 2, 3, 4};
}
flat gardenBOT
#
Compiler Output
<source>: In function 'main':
<source>:3:17: error: variable-sized object may not be initialized except with an empty initializer
    3 |     int xs[x] = {1, 2, 3, 4};
      |                 ^
Build failed
pearl edge
#

okay yeah apparently im going insane

#

;compile

const int x = 4;
int arr[x] = {1, 2, 3, 4};
flat gardenBOT
#
Compilation successful

No output.

pearl edge
#

okay

#

turns out im not insane

#

its a feature of c++ not c

#

mb

#

I knew one had it I just forgot which lol

#

C adopted the const qualifier from C++, but unlike in C++, expressions of const-qualified type in C are not constant expressions
c baited me with this one

#

When they are used as array sizes, the resulting arrays are VLAs

#

so yeah that specific answer is not true

old hawk
#

👍

delicate rock
#

I didn't know that, very cool

stone vapor
south surge