In C I typically create a function in the style of a constructor which does this. For example (error checking omitted for brevity)
Object* Object_new(int id, int value) { Object* p = malloc(sizeof(Object)); p->id = id; p->value = value; return p; } ... Object* p1 = Object_new(id++, myValue);
Related Posts:
- Initializing array of structures
- “error: assignment to expression with array type error” when I assign a struct field (C)
- How to initialize a struct in accordance with C programming language standards
- How to initialize array to 0 in C?
- Warning: assignment from incompatible pointer type
- How to properly malloc for array of struct in C
- What does “request for member ‘*******’ in something not a structure or union” mean?
- What does “request for member ‘*******’ in something not a structure or union” mean?
- Structure padding and packing
- Why and when to use static structures in C programming?
- error: struct has no member named X
- Initializing 2D char array in C
- Why should we typedef a struct so often in C?
- Why should we typedef a struct so often in C?
- dereferencing pointer to incomplete type
- How do you make an array of structs in C?
- Map like structure in C: use int and struct to determine a value
- error: expected primary-expression before ‘)’ token (C)
- Incompatible implicit declaration of built-in function ‘malloc’
- What is the cause of flexible array member not at end of struct error?
- waitpid, wnohang, wuntraced. How do I use these
- Sign extend a nine-bit number in C
- C: error: expected ‘)’ before ‘;’ token
- make: Nothing to be done for `all’
- Data argument not used by format strings in C
- execv vs execvp, why just one of them require the exact file’s path?
- difference between
and - expected expression before ‘{‘ token
- Setting std=c99 flag in GCC
- C Vector/ArrayList/LinkedList
- Returning an array using C
- warning: initializer element is not computable at load time
- error: ‘for’ loop initial declarations are only allowed in C99 mode [duplicate]
- c – warning: implicit declaration of function ‘printf’
- Flushing buffers in C
- What are 0x01 and 0x80 representative of in C bitwise operations?
- How to make parent wait for all child processes to finish?
- Undefined Reference issues using Semaphores
- What is time(NULL) in C?
- Use of cudamalloc(). Why the double pointer?
- Portable way to check if directory exists [Windows/Linux, C]
- C++ – Too Many Initializers for Arrays
- OpenGL — GL_LINE_LOOP —
- How to use EOF to run through a text file in C?
- Incorrect checksum for freed object on malloc
- Valgrind complains with “Invalid write of size 8”
- Expression must be a pointer to a complete object type using simple pointer arithmetic
- Simple way to check if a string contains another string in C?
- Segmentation fault (core dumped) due to fgets – I think
- Unknown ending signal when using debugger gdb
- Valgrind: invalid read of size 4 -> sigsegv, works fine without valgrind and in visual studio
- How to convert integer to char in C?
- Is there a good Valgrind substitute for Windows?
- Difference between sizeof(char) and sizeof(char *)
- Difference between “move” and “li” in MIPS assembly language
- How to empty a char array?
- What is the difference between array and enum in C ?
- Undefined reference to main – collect2: ld returned 1 exit status
- How can one see content of stack with GDB?
- What does the term “empty loop” refer to exactly in C and C++?
- Is there a way to have printf() properly print out an array (of floats, say)?
- What integer hash function are good that accepts an integer hash key?
- Proper way to empty a C-String
- malloc(): memory corruption
- How do you allow spaces to be entered using scanf?
- X86 assembly – Handling the IDIV instruction
- Pause screen at program completion in C
- Difference between fgets and fscanf?
- What is the difference between %g and %f in C?
- Can’t understand the working of getint() in C as per K&R
- Lua – Number to string behaviour
- too many initializers for ‘int [0]’ c++
- typedef struct pointer definition
- Reading in double values with scanf in c
- Realloc Invalid Pointer in C
- What tools are there for functional programming in C?
- typedef fixed length array
- “-bash: gcc: command not found” using cygwin when compiling c?
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- What are the differences between if, else, and else if?
- strtok segmentation fault
- Invalid write of size 1
- How to solve static declaration follows non-static declaration in GCC C code?
- realloc(): invalid next size when reallocating to make space for strcat on char *
- C Error: declaration shadows a local variable — Won’t let me repeatedly replace the value of my float variable
- MIPS to C Translation
- Pointer to 2D arrays in C
- Pointer to a string in C?
- gdb: No symbol “i” in current context
- Cross Platform C library for GUI Apps?
- Implicit function declarations in C
- Pre increment vs Post increment in array
- Convert long long to string in C?
- Using pointer to char array, values in that array can be accessed?
- How can I create a dynamically sized array of structs?
- how to stop a loop arduino
- How to normalize a mantissa
- How to use shared memory with Linux in C
- When is it ok to use a global variable in C?