438 questions
4
votes
3
answers
244
views
does calling a function means leaving the scope of an object declared prior to that call in C?
In the C17's final draft N2176 document, the 7th paragraph of 6.2.4 section says
For such an object that does have a variable length array type, its lifetime extends from the declaration of the ...
1
vote
1
answer
113
views
UB when passing a multi-dimension array of one form to function parameter expecting another
I'm looking at the C2Y draft n3467 and trying to wrap my heads around something involving functions taking VLAs.
It seems there's an undefined behavior that's not mentioned by the standard. That is:
...
0
votes
2
answers
77
views
C++ - Candidate function not viable: no known conversion from 'double[10][Globals::MAX_COL]' to 'double (*)[Globals::MAX_COL]'
When compiling my program, I get the following error:
No matching function for call to 'fillWithRandomNum'
Candidate function not viable: no known conversion from 'double[10][Globals::MAX_COL]' to '...
1
vote
0
answers
77
views
Why does the compiler allocate too much memory?
I have a bit of C code for an ARM Cortex-M0 chip, for which I was investigating the disassembly:
int func2(unsigned a) {
int h[a];
for (unsigned i = 0; i < a; i++) {
h[i] = 0;
}
int ch;...
20
votes
6
answers
2k
views
Where is the size of a VLA stored in C?
In C, you can do this:
int arr[i];
From what I understand, this is called a VLA—variable length array—which is an array whose size is not known at compile time, which in most implementations is ...
3
votes
2
answers
104
views
Specifying dimension of array in function and outside function does not give same result
This compiles fine under C99
const int DIM;
int main() {
int tab[DIM];
}
while the following gives the error
int tab[DIM];
int main() {
}
error: variably modified tab at file scope
Why?
I know ...
2
votes
1
answer
112
views
Is definition of variable length array (VLA) / known constant size recursive?
Issue: definition of variable length array (VLA) / known constant size seems to be recursive.
C11, 6.2.5 Types, 23 (emphases added):
A type has known constant size if the type is not incomplete and ...
5
votes
1
answer
140
views
What is the order of evaluation of VLA dimensions?
Is the following code:
#include <stdio.h>
void case1(int array[][printf("hello ")][printf("world ")]) {}
int i = 0;
void case2(int array[][i++][i++]) {}
int main(void) {
...
1
vote
1
answer
104
views
Support for `sizeof T[n]` in the Frama-C framework
I am wondering how difficult it would be to add rudimentary support for something like sizeof T[n] (which to my knowledge is an explicit C construct for a variable length array), to be used in a ...
-2
votes
2
answers
123
views
How do I store integers into 2 different arrays in C programming
The first array is multiples of 5, second array is multiple of 9. I was able to store the input but the printed array seems wrong as it did not print my supposed numbers. Any kind soul could help and ...
4
votes
1
answer
144
views
Check if array is a VLA at compile-time
@Lundin shows how to check if a passed expression is an array at compile-time here: Lvalue conversion of _Generic controlling expression involving array clang warning wrong?:
#define IS_ARRAY(T) ...
3
votes
3
answers
170
views
How to assign anonymous VLA to pointer?
Currently I am assigning a VLA to a pointer as follows
struct Foo {
int* array;
};
int array[size];
struct Foo foo = {
.array = array;
};
Is it possible to replace this with an "...
6
votes
2
answers
143
views
What are the exact conditions under which type_name in sizeof(type_name) is evaluated? GCC evaluates f() in sizeof(int [(f(), 100)])
Context
The standard says (C17, 6.5.3.4 ¶2):
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from ...
2
votes
3
answers
512
views
Why is stack memory usage in C++ determined at compile time?
I first started going down this rabbithole after learning that VLAs (variable length arrays) are not compatible with C++. This is due to the fact that an array of variable length would not have a size ...
0
votes
2
answers
127
views
What is the explanation of the odd behaviour in the size of the array in the given code? [duplicate]
#include<stdio.h>
int main(){
int n;
printf("Enter the number:");
scanf("%d",&n);
int array[n];
for (int i=0;i<=n;i++){
printf("%...
1
vote
1
answer
455
views
How do I construct an instance of a DST struct whose size is only known at runtime?
I have a struct that can be used in a fixed-size or unsized way, where the last element is an array of u8 items. We can write the following struct definition:
struct MyStructImpl<E: ?Sized> {
...
-1
votes
1
answer
82
views
clarifications on assembler code when declaring an array with a size decided at runtime
I'm trying to understand how sizeof() works, so I made 2 arrays, and see what is assembled, I've not used -O3 option because I thought the code to be clearer and the code is not deleted by ...
2
votes
3
answers
983
views
Return slice (or array of unknown size) from match expression
I'm trying to implement the following idea:
let command = ...;
let request = match subcommand {
Some(x) => [command, x as u8, (x >> 8) as u8],
None => [command],
};
request ...
0
votes
1
answer
256
views
Multiplot of variable length data using gnuplot
Here I have two data files with model parameters and RMSEs computed for them. I would like to get those RMSEs plotted for each model separately with the model also displayed on it.
Please help.
Data ...
0
votes
0
answers
113
views
Using a variable in format string of Fortran and display the values side-by-side [duplicate]
I understood through this post on how to use a variable in format string.
write(*,'(1x,f10.5)')(j(i),i=1,nvar) seems to work but the values in 'j' are displayed row-wise. I want them to be displayed ...
30
votes
2
answers
3k
views
Linux memcpy restrict keyword syntax
I know that the restrict qualifier in C specifies that the memory region pointed by two pointers should not overlap. It was my understanding that the Linux (not SUS) prototype for memcpy looks like -
...
1
vote
3
answers
131
views
Array using functions on C
A program to accept an array and diplay it on the console using functions.
Program should contain 3 functions including main() function.
Next two functions for accepting values to the array, and ...
0
votes
4
answers
128
views
C Dynamic Array Size
Hello I am new to coding and was just wondering why the below code:
#include <stdio.h>
int main() {
for(int i = 0; 1 ; i++) {
char x;
char z[1+i];
x=getchar();
if (x == '\n'){
...
0
votes
2
answers
179
views
C Use an array variable in a function parameter?
I have a 2D array of strings char* d2Array[valueA][valueB].
This works fine, I am able to fill it with strings and access them. There is an issue when passing it to a function. You need to specify the ...
0
votes
1
answer
103
views
Taking Input an array in C++ using cin directly by operator overloading
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
template<typename T, size_t Size>
std::istream& operator>>(std::istream& in, T (&arr)[Size])
...
-1
votes
2
answers
332
views
When to use Variable Length Array (VLA)?
malloc is slow and allocates on the heap. Good for large objects or
objects that need to get their lifetime extended beyond the function
where they are created.
static allocation is on the stack. ...
1
vote
2
answers
182
views
How do I store the sums of the elements in each row of a 2d array in 1d array with a function?
I have the following task: Create a 2d array A[M][N], where M and N are inputted by the user as well the elements in each row and column. There must also be two functions:
1)The first one needs to ...
0
votes
2
answers
140
views
How do I store integers in an array in c?
I am trying to make a programm that turns a string into binary.
int len = strlen(word);
int array2[len] = ascii_v(word, len);
int ascii_v(string w, int l)
{
int array1[l];
for (int i = 0, i &...
-2
votes
1
answer
91
views
Memory allocation for dynamic structs in C
Say I have a struct
struct GRAPH {
NODE* nodes[];
}
With a dynamic size for nodes[]. I also have
struct NODE {
char filename[40];
struct NODE* links[];
}
I will know how many links and nodes I ...
2
votes
2
answers
394
views
Asking user to input the sizes of 2D array C program
I'm coding a C program that asks the user to input two variables that represent the 2d array sizes and then print the array, I don't know where the problem is! any help?
`#include <stdio.h>
int ...
3
votes
3
answers
196
views
Taking sizeof of variable-length array — is there any benefit for doing so?
I am working on a piece of legacy code (no tests). I stumbled upon a section hidden inside several macros. It generates a warning if compiled with GCC's -Wvla.
The code in question is equivalent to ...
-1
votes
1
answer
82
views
I want to convert integer into an array in C and this is my code and it kind of works
Im learning C right now and I've got this problem I want to convert the integer into an array my code works but the problem is that I declare the size of an array in the beginning and I want to make ...
2
votes
1
answer
89
views
How do variable length arrays support numeric processing?
I am reading C The Complete Reference 4th edition by Herbert Schildt. In Chapter 4 - Arrays and Strings, it mentions "One major reason for the addition of variable-length arrays to C99 is to ...
0
votes
0
answers
111
views
Values in an Array within a Dynamic Array of Structs being overwritten by previous values
To preface: I am new to C and still learning the best way to efficiently code in C.
I'm trying to create an dynamic array of structs. I have an array within the struct however the values within the ...
0
votes
3
answers
1k
views
C How to create an 2d array of characters? [duplicate]
So would like to create an 2D array of characters for testing purposes. Here is my code.
const int rows = 4;
const int columns = 6;
//char field[rows][columns];
//fill_field(rows,...
7
votes
2
answers
159
views
What is the type of `1 ? 0 : (int(*)[f()])0`?
Probably, there is a contradiction is the C standard for VM types used in conditional operator. Assume:
int f(void) { return 42; }
Now, what is the type of a following expression?
1 ? 0 : (int(*)[f()]...
-1
votes
2
answers
108
views
How do I solve this array problem in C language?
I have 2 arrays:
int element[3] = {0, 1, 2};
int quantity[3] = {2, 3, 4};
Now I want a result array that will have two zeros, three ones and four twos.
int result[2+3+4] = {0, 0, 1, 1, 1, 2, 2, 2, 2};
...
0
votes
1
answer
117
views
With Construct-Package: RepeatUntil total lenght of subconstruct
Let's say I have:
numResults (Int16ul)
resultItems[numResults]
where the resultItems is constructed like:
ID (does not always increase)
strLen
my_str[strLen]
Now I understand, that I have to use ...
0
votes
1
answer
369
views
In C++, why can't I declare an array like this: int array[vector.size()]? [duplicate]
I want to convert a float vector to a float array in C++ 20. I searched online and found this solution:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
...
1
vote
1
answer
156
views
Error when dynamically allocating a 2D array - ISO C90 forbids variable length (C89/C90)
I'm trying to program up a 2D dynamically allocated array for a map for a small game. Below is my code, I'm unsure why int* array[yCoord] is throwing up the error that it cannot be a variable length ...
-2
votes
2
answers
81
views
C++ filling in array with input from another
I'm just taking input for two arrays and manipulating the information. When I take input for both arrays it puts the information from the second into both arrays. In the code below I haven even ...
-1
votes
2
answers
63
views
What is causing the segmentation fault in this code? Code is in C
I've been working in C lately and I've started working with arrays recently. Can you help me identify what's causing the issue here? I'm fairly certain the function inputArray is, but I cannot figure ...
0
votes
2
answers
86
views
this is the question "write a program in C to pass the reference of an array to a function & print that array" ,this is not giving the output ,why?
I got this question asked by one of my peer, as i don't know "C" that much, but still being a beginner i tried solving it, this is the
approach i used, but it is not giving the expected ...
1
vote
2
answers
869
views
struct with variable length array in std::variant
So I'm working with this struct type with variable length array member, like this:
struct Entry;
struct Data {
int members;
size_t entries_size;
Entry entries[1];
};
The entries_size ...
0
votes
3
answers
1k
views
Expression must have a constant value problem
I used C in Visual Studio to make a code for a user to input size of array.
The code does not work in Visual Studio and gives errors.
But on a site like replit it works.
I don't understand what to do ...
1
vote
3
answers
293
views
C++ Fibonacci number generator not working. Why?
I just wanna know why does this method to get the fibonacci number not work thanks.
#include <iostream>
#include <cctype>
#include <cmath>
using namespace std;
int fibonacci()
{
...
1
vote
3
answers
1k
views
How to create an n sized array in c
I am very new to C but I am having trouble on what seems a very trivial problem. All I am trying to do is create a n sized array, such that at the time of running I don't know its size and can't ...
0
votes
1
answer
163
views
How to create user define array structure of n size in c language
I want to create a 'n' size of array structure but I don't know how to do it. I always create static like this
typedef struct
{
int id;
char name[25];
char dob[11];
}info;
info student[5];
here ...
0
votes
1
answer
162
views
The difference between g++ and clang++'s handling of VLAs definition and initialization in c++
The source code as follows:
using namespace std;
int main()
{
int m = 4;
int arr[m] = {1, 2, 3, 4};
printf("%d\n", arr[2]);
return 0;
}
When I compile with ...
1
vote
3
answers
87
views
4 Dimensional Array - C
What is the problem with the following code? C compiler shows me error: Segmentation fault.
#include <stdio.h>
#include <math.h>
int main() {
int n = 4;
float A[n][n][n][n];
...