30,774 questions
9
votes
1
answer
535
views
variable name change causes segfault (datatype same name as the variable, used with malloc)
Okay it probably doesn't. That would be weird. I'm guessing, I'm allocating my memory wrong somehow.
#include <stdio.h>
#include <stdlib.h>
/*I have two Structs. In one there is a pointer ...
2
votes
1
answer
147
views
Trying to learn how getchar works
I am encountering a problem inside a while loop that is embedded in a for loop iterating through a struct of arrays. The issues I am coming across is before the array iterates again, I want to have ...
-1
votes
3
answers
125
views
sizeof operator appears to return wrong value for c type packed struct [duplicate]
Given the following C struct:
struct __attribute__((__packed__)) XIRControl
{
uint8_t command;
char code[19];
uint16_t error;
union codeData
{
struct rcvCommands
{
...
0
votes
0
answers
88
views
Why am I getting this error in my assembly program?
I'm making an assembly program that is supposed to get the mac address of the specified interface, however, when I try to assemble it, I get this error.
gethwaddress.s:12: warning: multi-line macro `...
-2
votes
3
answers
218
views
Can I declare a variable along with a pointer?
Can I declare a variable along with a pointer?
//like this
struct node n1,n2,n3,*start;
//or do i have to do it separately
struct node *start;struct node n1,n2,n3;
to declare variable alongside ...
2
votes
2
answers
199
views
derived class cannot call recursively inherited member functions C++
I'm making it using multi inheritance with a pair struct that contains
an index and the type of the element in a recursive structure like this:
template<int index, typename... ts>
struct ok;
...
1
vote
0
answers
71
views
Is it okay to leave a struct unnamed when using typedef? [duplicate]
I'm naming a struct type with typedef, and I've been using them like this
typedef struct {
double x, y, z;
} vect3;
However I found out in other headers such as Win32, that it tends to be like ...
4
votes
7
answers
280
views
How to define API in header and private fields in source in c
I want to define an API function in header file, and then implement it in source file.
Here is the hello.h, I want to define "world" in header:
typedef struct Hello {
int (*world) (void);...
-1
votes
0
answers
149
views
How do I create larger structs using tables in lua? (in the pico-8 game engine)
I am making a card game and im now revamping how individual card information is stored.
a single card would have the following values:
x, y, suit, number, state
this is to be stored in a deck array ...
0
votes
6
answers
276
views
Pointer to an array in a struct in C
I'm working (in C) with a struct that contains arrays of strings. How do I point to one of those strings?
To make it a bit clearer, consider:
struct Books {
char title[MAX1][MAX2];
char author[MAX1][...
3
votes
2
answers
170
views
Accessing a struct member from what is not the same struct type
The code below looks crazy, because I am interested in finding out the limits of what’s possible, and it isn’t exactly what I’m doing in my real code. It compiles without warnings and works as ...
-1
votes
0
answers
96
views
In C#, is an instance of a struct really an object? [duplicate]
I’ve been working with C# for 4 months. I’ve gained some experience, good and bad. Lately, I wanted to focus more on the concept of objects.
There’s a very important point that has been bothering me. ...
3
votes
1
answer
117
views
Compare structs in Odin
I have the following code in Odin:
Foo :: struct {
x: int,
y: int,
}
a := Foo{1, 1}
b := Foo{1, 1}
#assert(a == b)
According to official documentation I would expect to be able to compare ...
-2
votes
1
answer
64
views
What is the struct operator in SMT-LIB? [closed]
I want to create structures in SMT-LIB, like structs in C. So I coded this:
(set-option :print-success false)
(set-logic HO_ALL)
(declare-datatype |struct(Note : Z, Suffisant : BOOL)| (|record struct(...
-5
votes
2
answers
176
views
How to avoid heap allocation when calling interface methods on multiple struct types in a shared list [closed]
I'm writing a program that is very sensitive to garbage collection so I'm trying to avoid allocating to the heap after initialization.
In the example below, calling Run() will allocate to the heap ...
-2
votes
2
answers
136
views
Facing issues with Structs and malloc in an assignment question [closed]
This was a question in a C Programming assignment given to me as a part of last week's assessment. Requesting everyone to kindly explain what needs to be done and how it needs to be done. Kindly note ...
-3
votes
1
answer
73
views
How to declare a variant of a rust enum with a pre-existing variant outside of the enum?
I have code analogous to the following:
struct A(String);
impl A {
fn new(&str) -> Self
...
}
struct B(String);
impl B {
fn new(&str) -> Self
...
}
struct C(String);
...
1
vote
1
answer
110
views
how i can populate struct type only in returning joined column from sql
I have a table named tasks where have relationship one-many to another table (projects), i want to query tasks with projects but only return selected column from the projects table. how i can do that ...
10
votes
1
answer
907
views
Is CPP TrivialCopyable class effectively a C struct?
During coding of std::atomic, CAS, etc, I always struggle to memorize the definition of CPP class being "TriviallyCopyable".
Now I am gradually switching to C world, I accidentally found ...
4
votes
2
answers
138
views
Pass C struct as "deep" const
I have a struct which holds some pointers to data it does not own, e.g.:
struct s {
int* x;
int* y;
};
I can then pass it as a const pointer itself:
int foo(const struct s* s) {
return *s-...
6
votes
2
answers
264
views
How do I calculate the end address of a C struct in memory?
I have the following structure in my .c file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student {
char* name;
int age;
int id;
} Student;
...
0
votes
2
answers
165
views
I'm writing a parser in C++ for my programming language, but I don't figure out how to represent token trees [closed]
I want to create a parser in C++ for my programming language. Its logic is a little different than usual, because I want to first organize tokens in blocks (such as expressions or IDs, that are ...
1
vote
1
answer
108
views
Is [[__no_unique_address__]] applied to second members in this tail padding optimization check in libc++ redundant?
In the libc++ implementation of std::expected, there is this helper variable template:
// This function returns whether the type `_Second` can be stuffed into the tail padding
// of the `_First` type ...
1
vote
2
answers
147
views
Simulating (as far as possible) anonymous struct declaration + dynamic initialization
In some languages, we can declare and initialize an anonymous struct - or the equivalent thereof - seamlessly. Example from ECMAScript:
function foo_in_ecmascript(x) {
let computed = {
...
4
votes
1
answer
115
views
C struct assignment by unnamed struct initialized by member-wise list, syntax candy or unintentional memory overhead?
Prior statement: I'm a programmer long coding in C++ and Python, so kindly pardon and teach if my question looks too silly due to my thinking of C in an OOD way. Also I've tried lot searching on ...
15
votes
1
answer
916
views
What is correct mental model for [[no_unique_address]] in C++?
I recently found out about [[no_unique_address]] attribute in C++. According to cppreference.com:
Applies to the name being declared in the declaration of a non-static data member that is not a bit-...
2
votes
1
answer
180
views
Declareing array that stores a struct, which defined in another file included, gives the error "Incomplete Type "Array[]" is not allowed"
I have defined a struct in another file called file1.h (example) and when I declare an array using that struct in another file called File2.h, i get the error Message:
"Incomplete Type "...
0
votes
1
answer
111
views
tradingview! Persistent Pine Script v6 error: Syntax error at input 'end of line without line continuation'
It affects any struct. I've cleaned chars, renamed, tried new scripts – still fails.
Any solutions? So frustrating.
//@version=6
indicator("TestStruct", overlay=true)
struct MyData
float ...
1
vote
1
answer
157
views
How to efficiently parse data packets into structs
I am currently building functions that take packets of CAN data and parse them into structs depending on their ID as seen below.
Is there a 'cleaner' way of parsing data into structs instead of ...
4
votes
1
answer
122
views
(C) Accessing Arrays with Indices and Components
I'm writing a library for matrix and vector mathematics and I find it would be convenient to access vector components as if the vertex were both an array vector[2] and a struct vector.y, all while ...
0
votes
2
answers
126
views
Can't call struct function outputs in main function in C
I have a struct function which scans in coordinates, but when I call the function in my main function it doesn't seem to carry over any of the values and I get an error for 'use of undeclared ...
2
votes
1
answer
176
views
Does allocating a buffer smaller than the size of a struct cause undefined behavior, even if I do not reference any would-be out-of-bounds members?
Suppose I have the following:
struct S {
char b[256];
};
struct S *buf = malloc(128);
buf->b[0] = 1;
You may notice that the allocation size is smaller than the structure size, but at no ...
2
votes
2
answers
68
views
How to effciently add a field of the same repeated value to a struct array
I have a non-scalar struct array. For example
S=struct;
S(1).f1='a';
S(2).f1='b';
How do I add a field .f2 to S for the same value 1 efficiently? such that S becomes a struct with
S(1).f2=1;
S(2).f2=...
1
vote
3
answers
127
views
Default for a struct where most but not all fields implement default
I'm working on a project where I have a struct for which I want a default implementation.
#[derive(Debug)]
struct State {
view_type: ViewType,
booking_state: booking::State,
calendar_state:...
0
votes
0
answers
61
views
C++ ptr of a struct inside another struct, function return is problematic [duplicate]
In the following C++ code, three different structs are defined, each of which contains a pointer of each of other two structs. Intuitively, I would expect the output inside the function call and that ...
4
votes
1
answer
51
views
C Contact Manager: Email fields shift into next contact's data when loading from file
Problem Details:
I'm developing a C contact management system where each contact is stored as:
struct Contact {
char name[31];
char number[11];
char email[30];
};
Current Behavior:
When ...
1
vote
3
answers
116
views
How do I create an array of structs within a struct in C#?
How do I initialize an array of structs within a struct in C#?
public const int MAX_AXIS = 10;
struct realprm
{ /* real parameter */
long prm_val; /* value of variable */
long ...
3
votes
1
answer
140
views
Understanding sizeof for C Structures with bit-fields: alignment and packing
I am trying to understand how the sizeof operator works for C structures with bit-fields, particularly regarding packing and alignment. Below is a C program I wrote:
#include <stdio.h>
typedef ...
2
votes
0
answers
68
views
TypeLoadException when struct T's static property refers to T - why isn't it a compilation error? [duplicate]
Have a look at this short code (can be run as Program.cs):
var a = new MyStruct();
public record struct MyStruct
{
public static HttpRequestOptionsKey<MyStruct> OptionKey = new(nameof(...
0
votes
0
answers
34
views
Scilab struct crashes occasionally
The struct below crashes sometimes, but sometimes not !
I have executed the code on SciNotes, it runs fine, but when I give command SmokestackData in the Scilab console, Scilab crashes but not always.
...
4
votes
1
answer
158
views
How to avoid repeating yourself for structs with default values
#include <iostream>
#include <string>
#include <cassert>
struct Foo
{
std::string bar = "standard";
int x = 1;
int y = 1;
bool operator==(const Foo&) const = ...
2
votes
1
answer
251
views
Databricks explicitly insert null into struct column
I'm facing an issue when inserting records into struct field in Azure Databricks using SQL.
In my INSERT INTO statement I have a condition based on which I want to insert either null or a struct.
...
0
votes
1
answer
61
views
Validate length of all the fields inside an array of json
I need your help.
I have a problem, is that I need to verify if an element inside a JSON (share_id), which is in turn inside an array, has a legth greater than 20. There maybe be more than one set of ...
0
votes
2
answers
71
views
How to display a conditional row number in a list
I want to add a row number to a race list as shown above. Currently everyone is coming first. It's conditional as some people didn't finish (DNF).
So first I set up an ObservableObject:
class ...
1
vote
1
answer
65
views
How to write a template or generic type for similar data types with different size of type?
I wrote couple data types (struct) to store small arrays and copy them by values:
Array4 { base: int16|short, purpose: to store up to 4 values }
Array8 { base: int32|int, purpose: to store up to 8 ...
1
vote
0
answers
82
views
Using struct with overloaded operator `<` leads to `no match for ‘operator<’` error message when calling `std::set::insert` [duplicate]
I want to use std::set with a custom struct I've defined, similar to a question asked here. So in that case the struct needs an additional overload of the < operator to enable comparison. The ...
0
votes
1
answer
104
views
Reference to value in nested nullable structs
I am guessing the answer will eventually be "no, you can't do that", but trying to figure out how to do a thing in C#.
I am wondering on a bit of glue code between two pieces that I do not ...
1
vote
3
answers
166
views
Has C always required "struct" in front of a struct name, if the typedef with the same name was not declared?
In Modern C by Jens Gustedt, I read that
For historical reasons (again!), the name that we introduce for the structure always has to be preceded by the keyword struct, which makes its use a bit ...
0
votes
1
answer
149
views
Member "..." is not a typename, Constructor-definition (in .cpp) doesnt take struct as input variable
I'm trying to write some UI-Code (for learning purposes) and I get this error that I can't find a fix to.
The 4 errors are
`E0493` No instance of overloaded function "UIElement::UIElement" ...
2
votes
2
answers
95
views
How can we define nullable arrays in C structs which are allocated outside?
For optimization reasons, I'd like arrays in struct to be "nullable", so that I can allocate memory for it later. Like array parameters in functions.
This means, I need an array inside a ...