Created
October 22, 2021 18:22
-
-
Save maxgoren/aac5acc31db04bfaad044dc97b58f96d to your computer and use it in GitHub Desktop.
using a struct to return multiple values of different types in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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