Questions tagged [typedef]
In C and C++, the typedef keyword allows you to create an alias for a known data type.
2,959
questions
0
votes
1
answer
28
views
C - use the struct name as the parameter of function in strcut
I'm trying to implement stack in c. this is my code:
//header file
typedef struct
{
ElementType Data[MaxSize];
Position Top;
int Push(SeqStack *L);
}SeqStack;
and this is what the ...
0
votes
1
answer
80
views
Can you tell me what the less<pointer> does in this map typedef
I'm looking at some code written by someone else trying to bug fix and have this type definition
typedef map<RPacket *, CLocalLink *, less<RPacket *> > CPacketToLocalLinkMap;
I'm not sure ...
-2
votes
1
answer
68
views
Is there a way to dynamically access all variables of a given type?
I'd like to know if there is a way to access every variable of type INT so that I may perform a function on all of them.
In the example below, incrementing n number of integers.
Rudimentarily, my code ...
-1
votes
2
answers
32
views
MSVC - expression must have pointer-to-object type but it has type "float" on generic array?
MSVC on Visual Studio 2019 says "expression must have pointer-to-object type but it has type "float" on generic array" here:
void _stdcall sample::Eff_Render(PWAV32FS SourceBuffer, ...
0
votes
4
answers
47
views
How to nest structures?
typedef struct{
char name_cake[10];
char code_cake[10];
int stock_cake;
char about_cake[10];
char cake_taste[10];
}order;
typedef struct{
char name_cake[10];
char ...
0
votes
1
answer
22
views
Cannot restart typedef'd 2D array whose address is saved in struct
I have this typedef and struct:
typedef double mat[MAT_SIZE][MAT_SIZE];
typedef struct matList {
char *name;
mat *matrix;
} matList;
and I create an array of pointers to my structure:
...
1
vote
1
answer
75
views
Unable to refer to typedef struct definitions done in Win32 Header files (.h files in External dependencies) from WinRT C++ Library
Unable to refer to typedef struct definitions done in Win32 Header files (.h files in External dependencies) when consumed from WinRT C++ Library
#include <mfplay.h>
#pragma comment(lib,"...
2
votes
2
answers
79
views
How to have warning when casting `int_least8_t` to `char`?
I am building a string library to support both ascii and utf8.
I create two typedef for t_ascii and t_utf8. ascii is safe to be read as utf8, but utf8 is not safe to be read as ascii.
Do I have any ...
1
vote
0
answers
37
views
typedef in C++ where original name is same as alias [duplicate]
Based on my little bit of learning typedef is used to create an alias. But I see code where the "alias" is the exact same name as the original. What is the use here?
e.g.
typedef struct Dog{
...
1
vote
1
answer
39
views
Why structure and its typedef consume different size when aligned?
I was expecting both sizes are 8 but i get sizeof(myStruct) = 16, sizeof(myType) = 8. I compiled this on Windows 64-bit machine with MinGW.
#include <stdio.h>
#include <stdint.h>
struct ...
-1
votes
3
answers
112
views
Does declaring struct Name make Name equivalent to struct Name?
I am a bit confused when using struct in c/c++. Traditionally, when I use struct, I usually use it as:
typedef struct Name{
int a;
};
Name var;
Although it is considered a bad practice from ...
0
votes
3
answers
72
views
Is it required and/or preferred to wrap C enum items in parenthesis , and if so, what's the utility?
Which one is to use?
typedef enum
{
item_1 = 1,
item_2 = 5,
item_2 = 8,
item_2 = 10,
} formA_typeDef;
or
typedef enum
{
item_1 = (1),
item_2 = (5),
item_2 = (8),
item_2 = (10),...
0
votes
0
answers
28
views
C: get sizeof typedef struct array inside of function when passed as parameter [duplicate]
I have a bit of code here that I wrote that demonstrates a problem I am having.
I am getting a different value for sizeof when I do sizeof of a pointer that represents a struct array. I think that ...
0
votes
0
answers
19
views
":" sign in typedef struct definition [duplicate]
I am working on an embedded C application. I noticed that the below definition for typedef struct:
typedef struct _ARM_CAN_MSG_INFO {
uint32_t id; ///< CAN identifier with ...
3
votes
1
answer
54
views
instantiating a "function type" variable
In the 3 code snippets below, only the first one is accepted by the compiler, while the second and third aren't. I think all three cases are essentially doing the same thing: I am instantiating a ...
1
vote
2
answers
43
views
C11: Typedef of an anonymous struct works with pointers to the typedef as a member?
Here is how I would usually define a Node for a linked list in C:
typedef struct _Node {
int value;
struct _Node * next;
} Node;
With some testing, I have found that I am able to get this ...
3
votes
2
answers
38
views
How to use register storage class with typedef?
I want to do this:
typedef register unsigned int _newint;
Basically, a concise alias of register unsigned int as _newint.
But an error, "more than one storage class may not be specified," ...
1
vote
1
answer
27
views
Typedef in c, how it works when it takes 2 arguments
Can anyone explain to me how this code snippet works?
typedef int (*compare)(const char*, const char*);
0
votes
1
answer
27
views
Is template parameter stacking possible in Dart?
I'm currently trying to write a function that can take and execute any function with any set of parameters. In CPP, I do this using the following pattern;
template <class R, class... Args>
class ...
1
vote
2
answers
54
views
initialize array of structs with const
typedef struct{
int nim;
float ipk;
char nama[50];
char alamat[50];
} dataMahasiswa;
int main() {
dataMahasiswa p[MAX];
p[0] = (const ...
1
vote
1
answer
48
views
prototype of a struct in c
I'd like to organize my code in c, so it works a little bit like the public and private keywords in c++
specifically, I have some functions that will be used as a library in other projects, and I ...
0
votes
3
answers
130
views
Using "typedef" or "using" to define a structure - which is best?
Sample structure:
typedef struct tagExportSettings
{
COLORREF crHeading{};
COLORREF crEvenBack{};
COLORREF crOddBack{};
COLORREF crHighlight{};
COLORREF crDate{};
...
3
votes
2
answers
102
views
How can I declare multiple function pointer types in one typedef declaration?
I can do
typedef int a, b;
but I can't do something like
typedef void(*the_name_1, *the_name_2)(...);
Is there a way do to typedef 2 function pointer types at the same time ?
1
vote
1
answer
51
views
Typedef structure's member being static or not in c
I think this none sense doesnt that? because after creating instance from that variable. It will initialize that variable whole, will not behave differently to members an will initialize all of them, ...
2
votes
1
answer
61
views
Dealing with inconsistent typedefs in generic code
I routinely come across code in large codebases that do not follow the standard convention for typedefs e.g. ThisType instead of this_type.
Writing generic code where I can no longer rely on this_type ...
0
votes
1
answer
40
views
why the code can not compile after define my own structural type
Good morning.
I have a problem - I created definition of the myOwnType and I still have a problem with compile. Please help. C language (not C++).
main.c:
#include <stdio.h>
#include "...
1
vote
1
answer
57
views
What is the correct placement of names and types in the typedef syntax?
Usually the syntax of typedef is as follows
typedef <existing_name> <new_name>
But in the following case, I am bit confused
typedef char yes[1];
typedef char no[2];
This above seems ...
0
votes
2
answers
53
views
invalid use of incomplete typedef in C
I'm implementing a data structure in C and I get this error in my test file. Without adding code because then that would be a huge post with a ton of code to go through, but here's what my code looks ...
1
vote
2
answers
51
views
Invalid use of flexible array member error
typedef struct {
void *end;
void *start;
int size;
int arrs [];
} st;
void *doStuff(void *starter, void *ender) {
st *s = (st *) malloc(sizeof(st));
s-&...
1
vote
2
answers
268
views
How to properly use `typedef` for structs in C?
I see a lot of different typedef usages in many C courses and examples.
Here is the CORRECT way to do this (example from ISO/IEC C language specification draft)
typedef struct tnode TNODE;
struct ...
3
votes
1
answer
68
views
Does typedef (pointer to) VLA require evaluation of the size expression?
Does typedef VLA require evaluation of the size expression?
int f(void);
int main(void)
{
typedef int (T) [ f() ]; // is f required to be evaluated ?
T x;
return sizeof x;
}
Does ...
0
votes
2
answers
51
views
What does second argument here "typedef void fv(int), (*pfv)(int)"
Here is code which disturbing me:
typedef void fv(int), (*pfv)(int)
It seems it does definition of function fv which takes int as first argument, but what does mean here second part (*pfv)(int)?
1
vote
2
answers
36
views
How to Use Typedef Structure Name with Pointer
I am trying to implement a linked list using the given structure for a bigger project. The structure is defined below:
typedef struct node {
unint32_t size; // = size of the node
struct node * ...
0
votes
0
answers
30
views
query typedef alias in a template function
AFAIU I cannot overload a function with a typedef alias that has the same underlying type.
typedef int NumA;
typedef int NumB;
void func(NumA x) {}
void func(NumB x) {}
MSVC gives error C2084: ...
-1
votes
5
answers
98
views
Typedef in C, but a different way
I am aware of the standard way to define a structure in C. But, in analogy with this:
// <typedef structureName alias>
typedef unsigned long int u_int32
I was expecting this to work too:
struct ...
3
votes
1
answer
82
views
How do I do a forward declaration of a struct with a typename?
I am trying to do a forward declaration of a struct in c++ that has a typename. Something like this is entirely valid:
typedef struct foo foo;
struct foo{
int f;
};
My struct just instead has a ...
-1
votes
1
answer
21
views
structure initialization for xh711 issue
I am building a program for Xh711 on esp32. I have defined a a struct and i am trying to initialize it. I am getting the next error.
could not convert '{GPIO_NUM_18, GPIO_NUM_19, 0, 1, 1, GAIN64, 0, -...
0
votes
1
answer
51
views
How to use typedef to an array of function pointers
I have an array of function pointer:
int (*collection[2]) (int input1, int input 2) = {&fct1,&fct2}
I can get values by calling both functions from the array:
*collection[0](1,2);
*collection[...
5
votes
3
answers
89
views
Why can you create typedefs to a struct that doesn't exist?
The following code compiles fine.
header.h:
typedef struct Placeholder_Type* Placeholder;
impl.cpp:
#include "header.h"
void doSomething(Placeholder t) {
(void) t;
}
int main() {
...
1
vote
2
answers
79
views
How does typedefing influence compile/assembly?
Since there are two ways of writing enums, structs, unions or types where one uses typedef, or alternatively doesn't, I was wondering what would be the benefits and disadvantages of each approach.
E.g....
0
votes
4
answers
114
views
Does malloc assigns memory to custom struct's properties?
I've been working on some C projects and was wondering if I create a custom structure, for example, Student, define a variable of the custom structure type, and allocate memory to it using malloc, ...
-2
votes
1
answer
49
views
On the use of typedef keyword
typedef int ElementType;
struct SNode{
ElementType data;
struct SNode* next;
};
typedef struct SNode SNode;
typedef struct SNode* LinkStack;
typedef struct SNode* Position;
What role does typedef ...
1
vote
1
answer
72
views
Typedef Enum - Instantiation at top module and port connection
I am trying to instantiate a state machine at top level module and connect the states at top level ports to probe them as part of debug. I am trying something like this as in example below:
.state[0](...
1
vote
1
answer
74
views
Using typedef C like enum in python
I am a long-time C developer who is starting to use Python (more specifically 3.7.12) in some applications.
I am wondering if I can use something similar to the typedef enum structure that we have in ...
-2
votes
1
answer
65
views
C++: using-statement of forward-declared class generates error C2371
//A.h
class A;
//A.cpp
#include A.h
class MyA {};
using A = MyA; // error C2371 'A' : redefinition; different basic types
https://ideone.com/Aifxy2
A lot of legacy code uses the class name A.
I ...
0
votes
0
answers
67
views
How to typedef a template class with a pointer to a member variable
Suppose I have the following template class
template <typename T_Type, typename T_Class, T_Type T_Class::*ptr>
class Foo
{
using this_type = Foo<T_Type, T_Class, /* ??? */>;
};
how do ...
1
vote
3
answers
43
views
Content of array pointer is altered outside the loop
I'm initializing the values of an array pointer inside a function by allocating it first, and then allocating its content (struct pointers) iterating over the reading of values.
Those are the ...
2
votes
2
answers
66
views
Using typedef with wire in SystemVerilog
When using typedef to declare a user-defined type, both these forms are accepted by EDA Playground:
typedef reg[4:0] reg5;
typedef logic[4:0] logic5;
However, if doing something similar based on wire ...
1
vote
2
answers
82
views
using the current type in typedef in cpp
I need a data type which will hold either a string or a vector of the current data type. Here is a typedef I've written for that:
typedef std::variant<std::vector<value>, std::string> ...