Skip to content

Instantly share code, notes, and snippets.

@maxgoren
Created October 22, 2021 18:22
Show Gist options
  • Save maxgoren/aac5acc31db04bfaad044dc97b58f96d to your computer and use it in GitHub Desktop.
Save maxgoren/aac5acc31db04bfaad044dc97b58f96d to your computer and use it in GitHub Desktop.
using a struct to return multiple values of different types in c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct retVals {
double result;
char *str;
};
struct retVals* baz(int foo, int bar)
{
struct retVals* ret = malloc(sizeof(struct retVals));
double res = (double)foo/(double)bar;
char *str = "Just Because.";
int len = strlen(str);
ret->str = malloc(sizeof(char)*len);
strncpy(ret->str,str, len);
ret->result = res;
return ret;
}
int main()
{
struct retVals* foo = baz(8,3);
printf("%f - %s\n", foo->result, foo->str);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment