C++ Expected Constant Expression

Error C2057: expected constant expression

The sizes of array variables in C must be known at compile time. If you know it only at run time you will have to malloc some memory yourself instead.

programming in C: expected constant expression error in Visual Studio

You could allocate dynamically, use malloc or calloc. Note that by this way, you are allocating from the heap.

#include <stdlib.h>

const int howMany = 10;
int* goals = malloc(howMany * sizeof(int));

You should check the pointer in case malloc failed:

if (goals == NULL){
/* Something went wrong, act accordingly */
} else{
/* Move on in here, or just don't write the else part at all */
}

Then you can access this array by indexing:

goals[0] = 2017;

If you need to resize this array, you can use realloc. However while doing this, first use a new pointer and then check it again. Suppose you needed a bigger array in run-time. In this case, I will assume howMany wasn't declared as const int so it can be reassigned without some pointer hack.

howMany = 50;
int* temp_new_goals = realloc(goals, howMany * sizeof(int));
if (temp_new_goals == NULL){
/* Something went wrong again */
} else{
/* No problems, assign the new pointer. Don't worry, your old data remains still. */
goals = temp_new_goals;
}

At the end, don't forget to free the memory you allocated. You don't want memory leaks:

free(goals);

C++ expected constant expression

float x[size][2];

That doesn't work because declared arrays can't have runtime sizes. Try a vector:

std::vector< std::array<float, 2> > x(size);

Or use new

// identity<float[2]>::type *px = new float[size][2];
float (*px)[2] = new float[size][2];

// ... use and then delete
delete[] px;

If you don't have C++11 available, you can use boost::array instead of std::array.

If you don't have boost available, make your own array type you can stick into vector

template<typename T, size_t N>
struct array {
T data[N];
T &operator[](ptrdiff_t i) { return data[i]; }
T const &operator[](ptrdiff_t i) const { return data[i]; }
};

For easing the syntax of new, you can use an identity template which effectively is an in-place typedef (also available in boost)

template<typename T> 
struct identity {
typedef T type;
};

If you want, you can also use a vector of std::pair<float, float>

std::vector< std::pair<float, float> > x(size);
// syntax: x[i].first, x[i].second

Programming in C: Expected constant expression error

Your immediate problem is you are attempting to use a VLA (Variable Length Array), introduced into the standard with C99, with a compiler that does not support VLAs. Without VLA support, arrays must be declared with an integer constant (not simply a const int). As of C11, support for VLAs is optional.

To solve your immediate problem and provide portability, simply allocate storage for paths instead with malloc. The free the memory before you return from your function (either though an error return or on success)

You can do something like:

size_t pathlen = strlen(src) + 1 + strlen(name) + 2 + 1;
char *paths = malloc (pathlen); /* allocate storage for paths */
if (!paths) { /* validate EVERY allocation */
perror ("malloc-paths");
return 3; /* or however you want to handle the error */
}
char *suff = stpcpy(path, src);

...

*head = fopen(path, "wx");
if (*head == NULL) {
free (path); /* free paths */
return errno == EEXIST ? 2 : 1;
}

...

if (*code == NULL) {
int err = errno;
free (path); /* free paths */
fclose(*head);
*head = NULL;
*suff = 'h';
unlink(path);
return err == EEXIST ? 2 : 1;
}

free (path); /* free paths */
return 0;

There is a small overhead for the allocation and free, but it is negligible in your case where there is a single allocation and single free.

c++: expected constant expression

Declare a const int:

int main() 
{
const float a = 0.5f;
const float b = 2.0f;
const int s = static_cast<int>(a * b) + 1;

int array_of_ints[s];
return 0;
}

Example

Note that this works on the oldest compiler I have access to at the moment (g++ 4.3.2).

Can't create array with constant size ( expected constant expression”)

I know the size of arrays needs to be known at compile time, but surely that's the case here.

That's actually not the case. In C, const qualifying doesn't result in a "constant expression". So newsize isn't a constant expression (unlike C++).

Your code is valid in C99 and in C11, if variable length array (VLA) is supported by your implementation (VLAs are optional in C11). However, it seems Visual studio doesn't seem to support VLAs and expects a "constant expression" for array size as in C89.

So you may have to use dynamic memory allocation (malloc & friends), or simply specify the 100 as size, or use a macro for defining size and so on.

VS2008 error expected constant expression on declaring array, but no error for this code in GCC

size is not a constant expression. The term "constant expression" in the error message refers to the C++ concept of Integral Constant Expression. The whole idea of Integral Constant Expression is that its value should be known at compile time. For example, an integral const object declared with an initializer can be used as an Integral Constant Expression.

Just because you declared some int variable const does not turn it into an Integral Constant Expression. Function parameters never form Integral Constant Expressions. This is why you cannot use your size to define array size in C++.

GCC compiles your code because it brings over a C-specific feature from C language to C++ as a non-standard extension. Note that in GCC your size is not considered constant either. GCC simply does not require array sizes to be constant.

If you switch your GCC compiler into strict and pedantic C++ mode, it will refuse to compile your code just as MSVC++ does.



Related Topics



Leave a reply



Submit