Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Browsing the source code, I came across something completely unexpected. I didn't know you could put code in structs like this sample from kamby/kamby.c

  struct KaNode *ka_add(struct KaNode *node, struct KaNode **env) {
    if (node->type == KA_STR && node->next->type == KA_STR) {
      struct KaNode *output = ka_str(node->str);
      strcat(output->str, node->next->str);
      return output;
    }
    return ka_num(node->num + node->next->num);
  }
Is this legal? If so, I'm going to use the heck out of it for my version of STOIC.

[Edit] Ok.. thanks for the help.. I'm used to seeing function types declared AFTER the parameters because pascal habits die hard. It looked like a run of the mill structure declaration to me.



I don't do a lot of C, but isn't this just a global function returning a pointer to a KaNode? If I remember correctly this is just Cs way of specifying a type is a struct same thing with the line in your example "struct KaNode *output = ..." someone feel free to correct me if I am wrong though.


Would be easier to read if KaNode was a typedef


That’s a function that returns a struct. In C structs live in their own namespace and references to them have to use the struct keyword unless you define a typedef.


That's not a struct. It's a function called ka_add that returns a pointer to a KaNode


If it makes you feel any better, I can't remember the last time I saw that construction in the wild. Idiomatic C codebases use a typedef for this 10 times out of 10.


I don't really know C, but I think this is a function returning a "struct KaNode*" type, not a struct.


Lol, that's a function that returns a pointer to a struct.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: