Skip to content

Instantly share code, notes, and snippets.

@keikoro
Created March 19, 2014 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keikoro/9640812 to your computer and use it in GitHub Desktop.
Save keikoro/9640812 to your computer and use it in GitHub Desktop.
C - return multiple (string) values
/* Output multiple (string) values
defined in a function
to main function.
Based on solution #2 of accepted answer in this Stack Overflow thread:
https://stackoverflow.com/questions/2620146/
how-do-i-return-multiple-values-from-a-function-in-c
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void getValues(char** a, char** b) {
/* Make sure a and b are not pointing to NULL */
assert(a);
assert(b);
*a = "hello";
*b = "world";
}
int main() {
char* a = (char*)malloc(sizeof(char)*256);
char* b = (char*)malloc(sizeof(char)*256);
getValues(&a, &b);
printf("a is %s\nb is %s\n", a, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment