r/C_Programming • u/Xaneris47 • May 20 '26
r/C_Programming • u/HouseMFD • May 20 '26
Question How to read/write a struct into a binary file?
I've tried to follow this answer on SO: https://stackoverflow.com/a/16997175/1984657
However I'm not able to load my character successfully. I do get something back, but when I try to print the name I get a string of unrecognizable characters.
My struct is:
struct Character
{
char name[8];
enum CharClass charClass;
short level;
};My logic to load or create the character:
#include <stdio.h>
#include <string.h>
#include "character.h"
#include "save_file_manager.h"
struct Character *startup()
{
struct Character *loadedCharacter = loadPlayerCharacter();
if (loadedCharacter != NULL)
{
printf("Welcome back, %s the %s\n", loadedCharacter->name, getClassString(loadedCharacter->charClass));
return loadedCharacter;
}
struct Character createdCharacter;
struct Character *createdCharacterPtr = &createdCharacter;
char name[NAME_LENGTH];
int charClass;
// logic to get name and charClass from user input
strcpy(createdCharacterPtr->name, name);
createdCharacterPtr->charClass = charClass;
createdCharacterPtr->level = 1;
return createdCharacterPtr;
}And my save, load and prompt to save functions are:
#include "save_file_manager.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
const char *SAVE_FILE_NAME = "./textrpg.sav";
int savePlayerCharacter(struct Character *character)
{
FILE *fptr;
fptr = fopen(SAVE_FILE_NAME, "wb");
fwrite(character, sizeof(struct Character), 1, fptr);
fclose(fptr);
return 0;
}
struct Character *loadPlayerCharacter()
{
FILE *fptr;
fptr = fopen(SAVE_FILE_NAME, "rb");
if (fptr == NULL)
{
return NULL;
}
struct Character *character = malloc(sizeof(struct Character));
int amountRead = fread(character, sizeof(struct Character), 1, fptr);
if (amountRead != 1)
{
return NULL;
}
fclose(fptr);
return character;
}
int promptSave(struct Character *playerCharacter)
{
printf("Would you like to save your character? (y/n)\n");
char save;
scanf(" %c", &save);
if (save == 'y')
{
savePlayerCharacter(playerCharacter);
}
return 0;
}I probably missed something obvious, but I don't see it. What am I doing wrong?
Solution: I'm allocating heap memory to the created character in startup now:
struct Character *createdCharacterPtr = malloc(sizeof(struct Character));Thank you everyone for your help.
r/C_Programming • u/Straight_Coffee2028 • May 20 '26
I built a DSA library in C and it got selected for an open-source program, looking for contributors
I built C_DSA_interactive_suite, a fully modular, console-based DSA library written entirely in pure C11. No frameworks, no abstractions. Just raw C, manual memory management and real implementations built from scratch. The codebase has a clean modular architecture with one .h/.c pair per module, reusable APIs across modules. A single interactive executable as the entry point, Valgrind-clean memory, CI/CD on every push, and .clang-format enforced style throughout. The project just got selected in SSOC, an open-source program similar to GSoC and I am the project admin, which means this summer, project is open for contributiors! If you've been wanting to:
- Start your Open Source journey
- Contribute to a real C codebase with proper architecture and planning
- Implement something like AVL trees, heaps, priority queues, tries, Dijkstra, or DP
- Get your hands dirty with actual C, not school level C
- Have your contribution acknowledged by a reputable community
This is your shot. Everybody else is doing MERN. Come touch some real memory. I'm available to walk anyone through the codebase, clear doubts, explain concepts, or help you get your first PR merged. No gatekeeping. Register as contributor for SSOC. Only 5 days left - https://www.socialsummerofcode.com/ GitHub: https://github.com/darshan2456/C_DSA_interactive_suite Drop a comment or open an issue if you're interested. Let's build something worth putting on a resume.
here is my personal discord server link - https://discord.gg/MWv949G8h join it if you want to contribute to my project in any way
r/C_Programming • u/SeaInformation8764 • May 20 '26
Question Why Don't Double void Pointers Match Like Single void Pointers?
In the following code, calling test1 doesn't provide a warning, but calling test2 does. Is there a specific reason for this behavior?
void test1(void* x);
void test2(void** x);
int main() {
int* x1;
int** x2;
test1(x1); // ok
test2(x2); // warning "incompatible pointer type"
}Edit: I think some people don't understand. I'm asking why void** doesn't match with other x**, not why it doesn't work exactly the same as void*.
r/C_Programming • u/pjl1967 • May 20 '26
My own bare-bones dynamic array
About a month ago, somebody posted asking for design advice for a dynamic array. My then advice was to treat the element type as an opaque type T.
I've had my own implementation of such a dynamic array lying around for a while, but finally had a use for it, so I gave it a bit of polish and it's here:
If you wanted it to be even more bare-bones, you could keep only the regular (bounds-checking) functions or the no-check (_nc) functions, whichever you prefer.
r/C_Programming • u/Undeniable_Dilemma_ • May 19 '26
Question Any Recommendations on C resources for Learning Vulkan?
I was following this tutorial, which is supposed to be "modern" with "up to date" features and practices.
However, I very quickly stumbled upon this piece of code:
auto unsupportedLayerIt = std::ranges::find_if(requiredLayers,
[&layerProperties](auto const &requiredLayer) {
return std::ranges::none_of(layerProperties,
[requiredLayer](auto const &layerProperty) { return strcmp(layerProperty.layerName, requiredLayer) == 0; });
});I'm just stunned. I can't believe cpp people actually write code like this? This just seems insane to me and I kind of hate it.
So I have 2 questions:
- Am I in the wrong here? Should I just accept that this is normal and the industry standard and get over it? Or is it just absurd?
- If it IS absurd, does anyone have any recommendations for other resources I could learn Vulkan from? I'd prefer if it was just straight up in C or at least very basic features of C++?
r/C_Programming • u/Senior-Question693 • May 19 '26
What's up with drag&drop in Wayland?
I'm making a file manager from scratch and i can't figure out the drag&drop functionality, after hunting for docs on the internet i found (this)[https://emersion.fr/blog/2020/wayland-clipboard-drag-and-drop/] blog-post, but it mentions only text, what about files (like when you drag&drop a markdown file into a browser to read it or when you drag&drop a video into a player)? Surely it's gotta be different (at least the mime type in the send and cancel callbacks) but i can't figure out in what way, here is the code i came up with:
typedef struct wayland_data wayland_data;
void
void
static const struct wl_registry_listener registry_listener = ;
static void
static void
static const struct wl_data_source_listener data_source_listener = ;
int
(keep in mind that it's not the final code yet and there is no send and cancel functions)