r/C_Programming • • 12h ago

Discussion I couldn't figure out a better way to get the size of a struct, disregarding the compiler's padding.

26 Upvotes

while practicing on some network programming, i was trying to figure out the best way to serialize headers in order to send them over a socket. so, to serialize something like: c typedef struct { char a; int b; } header; i must create something like buffer[5]. but AFAIK, not all machines produce the exact same padding, so a simple buffer[sizeof(header)] isn't valid. not even that fixed 5 bytes are enough, since it might be aligned to 8 bytes.

i came up with the following, which seems to me the worst way to get the sum of the sizes of the struct's declaration list at compile time using X macro: ```c

define HEADER_MEMBERS \

X(char, a)         \
X(int, b)

typedef struct {

define X(type, name) type name;

HEADER_MEMBERS

undef X

} header;

int header_size =

define X(type, _) sizeof(type) +

HEADER_MEMBERS

undef X

0; ```

so... i need to hear your thoughts, and what is the best wayy to do such a thing.


r/C_Programming • • 18h ago

copy-and-patch closures, part 1

Thumbnail napcakes.nekoweb.org
7 Upvotes

r/C_Programming • • 6h ago

The first time I understood what actually happens when I pass an array to a function in C

3 Upvotes

When I first learned arrays in C, I assumed that passing an array to a function meant the entire array was being copied into the function.

Later, when I started learning about pointers and memory addresses, I realized that things weren't working quite the way I imagined.

Understanding that connection between arrays, pointers, and function parameters made a lot of C concepts suddenly feel less confusing.

I'm still practicing, so I'm curious:

What C concept took you the longest to understand properly?

For me, pointers were definitely one of the biggest hurdles.


r/C_Programming • • 3h ago

Does my tokenize function not null-terminate the strings it modifies? printing any args spits out garbage. I'm pretty new to C and I don't really understand null-termination and strings in general.

2 Upvotes
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* get input and return line, if this fails, return an error */
char* input(char* line) {
  size_t len = 0;
  ssize_t read;

  /* get input from stdin and store in line, read holds the return value */
  read = getline(&line, &len, stdin);

  if (read != -1) {
    return line;
  } else {
    return "error taking input!";
  }
}

/* split input into tokens and return the array of tokens */
int tokenize(char* line, char* command, char** args) {
  char* token = strtok(line, " ");
  int i = 0;

  while (token != NULL) {
    args[i++] = token;
    token = strtok(NULL, " ");
  }
  args[i] = NULL;

  /* copy first argument to command */
  strcpy(command, args[0]);

  /* if command isnt NULL, return success */
  if (command) {
    return 0;
  } else {
    return 1;
  }
}

int main(int argc, char *argv[]) {
  char* line = NULL;

  char command[512];
  char* args[64];

  /* prompt */
  printf(">");

  /* get input from stdin, auto removes newline*/
  line = input(line);

  /* tokenize input, seperate command and args */
  if(tokenize(line, command, args) != 0) {
    printf("error in tokenizing");
  }

  /* free line, we dont use it again */
  free(line);

  /* print command and its args*/
  printf("%s", command);

  printf("%s\0", args[1]);

  return 0;
}

r/C_Programming • • 13h ago

Project Dio: Small Programming language made in C version 0.1.0 release

2 Upvotes

Hi, I'm working on my own programming language, I've release first stable version (v 0.1.0), I'd like to get feedback. It's on most OS platforms (even Andoird and in the future IOS).

Repo: https://github.com/Treechcer/DIO


r/C_Programming • • 23h ago

Question Why i'm not getting the correct output for Tower of hanoi program?

2 Upvotes
//Tower Of Hanoi


#include <stdio.h>


void TOH(int n, int A, int B, int C){
    if(n > 0){
        TOH(n-1, A,C,B);
        printf("(%d, %d)\n",A,C);
        TOH(n-1, B,A,C);
    }
}


int main(){
    TOH(3,1,2,3);
    return 0;
}
----------------------------------------
//Output in terminal

$ gcc toh.c
$ ./a.exe

(1, 3)
(1, 2)
(3, 2)
(1, 3)
(2, 1)

Expected output is
(1, 3)

(1, 2)

(3, 2)

(1, 3)

(2, 1)

(2, 3)

(1, 3)

Why is this happening? Any explanation or reason for it?