returning multiple values from a function in C

hi

how can I return multiple values from a C function. I tried the following:

#include <stdio.h>
void foo(int id, char *first_name, char *last_name)
{
/*
  this is just an example to illustrate my problem... real code makes
  use of the "id" parameter.
*/
   first_name = (char *)malloc(99 * sizeof(char));
   strcpy(first_name, "john");
   last_name = (char *)malloc(99 * sizeof(char));
   strcpy(last_name, "doe");
printf("----inside foo(): first name is: [%s]\n", first_name);
printf("----inside foo(): last name is: [%s]\n", last_name);

 
} /* foo() */

main() 
{
  char *fname, *lname;
  
  foo(4, fname, lname); 

  printf("x-----------------\n");
  printf("first name is: [%s]\n", fname);
  printf("y-----------------\n");
  printf("last name is: [%s]\n", lname);

}

however, I get a coredump:

----inside foo(): first name is: [john]
----inside foo(): last name is: [doe]
x-----------------
Segmentation Fault (core dumped)

how can I get the fname and lname variables in the main() function to hold the values initialized
by the foo() function? i tried:

 foo(4, &fname, &lname);

but this results in:

foo.c:19: warning: passing arg 2 of `foo' from incompatible pointer type
foo.c:19: warning: passing arg 3 of `foo' from incompatible pointer type

first ,you don't use the int ID int fuction foo,
second ,you malloc fname and lname in a fuction(although they are pointers ,but you know ,you can't get a val from a fuction by pass it a pointer ) , int the main ,you don initial them, so the result is random

i mentioned in the code comment that this is only a demo and that 'id' is used in my real code.

anyway, i did a malloc() in main. this got rid of the coredump, but still
there are no values assigned to lname and fname.

#include <stdio.h>
void foo(int id, char **first_name, char **last_name)
{
/*
  this is just an example to illustrate my problem... real code makes
  use of the "id" parameter.
*/
   *first_name = (char *)malloc(99 * sizeof(char));
   strcpy(*first_name, "john");
   *last_name = (char *)malloc(99 * sizeof(char));
   strcpy(*last_name, "doe");
printf("----inside foo(): first name is: [%s]\n", *first_name);
printf("----inside foo(): last name is: [%s]\n", *last_name);

 
} /* foo() */

main() 
{
  char *fname, *lname;
  
  foo(4, &fname, &lname); 

  printf("x-----------------\n");
  printf("first name is: [%s]\n", fname);
  printf("y-----------------\n");
  printf("last name is: [%s]\n", lname);

}

i run it in my reahat ,it's ok!

One way to return multiple values from a function is to return a structure whose members are the variables that you want to return...

struct ts
{
   char *fname;
   char *lname;
}

With this struct definition you can pass the entire struct to foo and return it back to main and it is not malloc that causing the core dump but the fact that fname and lname in main do not point anywhere unlike in foo where fname points to "john" and lname points to "doe". By passing the struct between main and foo the program won't crash.

#include <stdio.h>

void foo(int id, char **first_name, char **last_name)
{
/*
  this is just an example to illustrate my problem... real code makes
  use of the "id" parameter.
*/
//   *first_name = (char *)malloc(99 * sizeof(char));
   strcpy(*first_name, "john");
   strcpy(*last_name, "doe");
 //  *last_name = (char *)malloc(99 * sizeof(char));
printf("----inside foo(): first name is: [%s]\n", *first_name);
printf("----inside foo(): last name is: [%s]\n", *last_name);


} /* foo() */

main()
{
  char *fname, *lname;

  fname = (char *)malloc(99 * sizeof(char));
  lname = (char *)malloc(99 * sizeof(char));
  foo(4, &fname, &lname);

  printf("x-----------------\n");
  printf("first name is: [%s]\n", fname);
  printf("y-----------------\n");
  printf("last name is: [%s]\n", lname);

}

This is not an issue related to where we do memory allocation. Look now I have done memory allocation in main and getting the expected result.

Correct it's not related to where you allocate memory in main or in foo as long as the pointers to the malloced memory are kept in sync. If memory is malloced in foo and pointers in main are not aligned with those in foo it will coredump...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

typedef struct
{
   char *fname;
   char *lname;
} ts;

ts *foo(int id, ts *xp)
{
   xp->fname = (char *)malloc(99 * sizeof(char));
   strcpy(xp->fname, "john");
   xp->lname = (char *)malloc(99 * sizeof(char));
   strcpy(xp->lname, "doe");
   printf("----inside foo(): first name is: [%s]\n", xp->fname);
   printf("----inside foo(): last name is: [%s]\n", xp->lname);
   return xp;
}

int main() 
{
   ts x, *px;
   px = foo(4, &x);
   printf("x-----------------\n");
   printf("first name is: [%s]\n", px->fname);
   printf("y-----------------\n");
   printf("last name is: [%s]\n",px->lname);
}

I see what i did wrong.
thank you all for your responses.

--Andrew