438 questions
4
votes
3
answers
227
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 ...
0
votes
2
answers
76
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 '...
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 ...
2
votes
1
answer
106
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 ...
1
vote
1
answer
102
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 ...
4
votes
1
answer
141
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) ...
6
votes
2
answers
141
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 ...
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
votes
1
answer
79
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 ...
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 ...
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 -
...
0
votes
4
answers
124
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
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
vote
2
answers
180
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 ...
-2
votes
1
answer
90
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 ...
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 ...
2
votes
1
answer
87
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
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,...
-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
366
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()
{
...
-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 ...
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 ...
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
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
160
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 ...