variable-length-array

Initialize array with a non-const function argument

让人想犯罪 __ 提交于 2019-12-24 14:05:36
问题 Is there any way to initialize an array with a non-const integer, or to make the existing variable constant in order to make it a valid argument? bool f( const char s[], const int n ) { char c[n]; // error: expression must have a constant value } 回答1: No, not in the general case. Use vector<char> c(n) instead. Simplified, almost correct explanation: if you don't know what n is at compile time, neither does the compiler. So it cannot put aside memory for the array. This is why vector exists.

Are conformant array parameters VLAs?

江枫思渺然 提交于 2019-12-24 10:39:41
问题 CERT's Secure Coding Standard includes an item (API05-C) which encourages the use of conformant array parameters, which is a recommendation I've implemented in a lot of my code (hidden behind a macro for compilers which don't support them). For those who don't know, a conformant array parameter is something like: void foo(int length, char data[length]); API05-C provides more information. Lots of compilers don't like variable-length arrays (for good reason). C11 demotes them from required (as

Code:Blocks Mingw Compiler Error: Variable-Sized Object May Not Be Initialized

我只是一个虾纸丫 提交于 2019-12-23 21:14:01
问题 I am creating a simple terminal fantasy game using C++. I have seemed to run into an error "error: variable-sized object 'items' may not be initialized". Here is the code: string useItem(int item) { string items[item] = {"HP Potion","Attack Potion","Defense Potion","Revive","Paralize Cure"}; } I want to be able to use this function in order to access and return an item. How can I fix this error. I am using Code::Blocks with mingw compiler. 回答1: There are a couple of issues here, one variable

Returning a variable length array from a function

杀马特。学长 韩版系。学妹 提交于 2019-12-23 06:12:42
问题 The following function "increment" add 1 to a number represented as an array. int* increment(int array[], int size, int *sizeLen) { int temp[size+1]; int carry = 0; carry = (array[size-1]+1)/10; temp[size] = (array[size-1]+1)%10; for(int i=size-2;i>=0;i--) { temp[i+1] = (array[i] + carry)%10; carry = (array[i]+carry)/10; } if(carry) { temp[0] = 1; *sizeLen = size+1; return temp; } else { *sizeLen = size; return (temp+1); } } int main() { int array[] = {9,9,9}; int length; int *res = increment

Returning a variable length array from a function

 ̄綄美尐妖づ 提交于 2019-12-23 06:12:16
问题 The following function "increment" add 1 to a number represented as an array. int* increment(int array[], int size, int *sizeLen) { int temp[size+1]; int carry = 0; carry = (array[size-1]+1)/10; temp[size] = (array[size-1]+1)%10; for(int i=size-2;i>=0;i--) { temp[i+1] = (array[i] + carry)%10; carry = (array[i]+carry)/10; } if(carry) { temp[0] = 1; *sizeLen = size+1; return temp; } else { *sizeLen = size; return (temp+1); } } int main() { int array[] = {9,9,9}; int length; int *res = increment

VLAs and side-effect in sizeof's operand

霸气de小男生 提交于 2019-12-20 17:29:23
问题 I know that sizeof never evaluates its operand, except in the specific case where said operand is a VLA. Or, I thought I knew. void g(int n) { printf("g(%d)\n", n); } int main(void) { int i = 12; char arr[i]; // VLA (void)sizeof *(g(1), &arr); // Prints "g(1)" (void)sizeof (g(2), arr); // Prints nothing return 0; } What is going on? Just in case, this is compiled with GCC 5.1 on Coliru. 回答1: It seems that I should think twice before posting, because it struck me right after I did. My

Can I use a C Variable Length Array in C++03 and C++11?

微笑、不失礼 提交于 2019-12-20 01:58:32
问题 C has a really cool feature called variable length arrays. Its available in C90 and above, and it allows deferring the size of the array until runtime. See GCC's manual 6.19 Arrays of Variable Length. I'm working in C++. At std=c++11 , I'm catching a compile failure due to the use of alloca under Cygwin. I want to switch to variable length arrays, if possible. I also want to try and avoid std::vector and std::array because I want to stay out of the memory manager. I believe every little bit

Why doesn't work Variable length array as a globally? [duplicate]

谁都会走 提交于 2019-12-19 09:16:16
问题 This question already has answers here : declaring a variable-length array as a global variable in C (4 answers) Cannot compile static array with fixed size [duplicate] (1 answer) Closed last year . If I write variable length array as a locally, like this: #include <stdio.h> int main() { int i=1; int a[i]; printf("%zu",sizeof(a)); } It working fine in GCC compiler. But, If I write Variable length array as a globally, like this: #include <stdio.h> int i=1; int a[i]; int main() { printf("%zu"

Variable Length Arrays in C++14?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 04:54:22
问题 n3639 proposed the adoption of c99's variable-length-arrays into C++14 (at least for the first dimension.) But the latest I've been able to find lists n3639 as: Features in the first CD of C++14, subsequently removed to a Technical Specification Did this ever make it into a Technical Specification, or was it lost in the hand off? The reason for my question is, I've noticed this code: void f(size_t n) { int a[n]; for (size_t i = 0; i < n; ++i) a[i] = 2 * i; sort(a, a + n); } This fails to

Prototype for variable-length arrays

☆樱花仙子☆ 提交于 2019-12-18 03:40:57
问题 I am trying to write a function that takes an array of an variable size in c. void sort(int s, int e, int arr[*]){ ... } It says that for variable length arrays, it needs to be bounded in the function declaration. What does that mean? I am using xcode 4.0, with the LLVM compiler 2.0. Thanks for the help. 回答1: If you're not using the C99 variable length arrays, the usual solution is to pass in a pointer to the first element, along with any indexes you want to use for accessing the elements.