returning multiple values from a function in C


 
Thread Tools Search this Thread
Top Forums Programming returning multiple values from a function in C
# 1  
Old 01-17-2009
returning multiple values from a function in C

hi

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

Code:
#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:

Code:
----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:

Code:
 foo(4, &fname, &lname);

but this results in:

Code:
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

# 2  
Old 01-18-2009
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

Last edited by aobai; 01-18-2009 at 12:42 AM..
# 3  
Old 01-18-2009
returning multiple values from a function in C

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.
# 4  
Old 01-18-2009
Code:
#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);

}


Last edited by Franklin52; 01-18-2009 at 10:32 AM.. Reason: adding code tags
# 5  
Old 01-18-2009
i run it in my reahat ,it's ok!
# 6  
Old 01-19-2009
One way to return multiple values from a function is to return a structure whose members are the variables that you want to return...
Code:
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.
# 7  
Old 01-19-2009
Code:
#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.

Last edited by Franklin52; 01-21-2009 at 01:30 PM.. Reason: adding code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash] wanted: function with a clean way for multiple return values

Hi, I have a small part of a project which is done as a bash script. bash was selected as an portability issue that works out of the box. In this script I have an exec shell-function, a wrapper around arbitrary commands. I want to have STDOUT, as an addon STDERR and the EXIT-CODE of a specified... (5 Replies)
Discussion started by: stomp
5 Replies

2. Shell Programming and Scripting

Returning multiple values in Shell

Hi I have a code as the following #!/usr/bin/ksh set -x row() { a=$1 b=$2 c=$(($a + $b)) d=$(($a * $b)) echo $a $b } e=`row 2 3` set $e echo "The value of c is $c" echo "The value of d is $d" My requirement is I need to pass two arguments to a function and return two values... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

3. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Output breaking when returning multiple values

I've been trying to write a command-line function to grab a website's MX records and their ip addresses. The code below works with domains that only have one MX record: function kmx { mx=`host -t MX $1 | awk '{ print $7 }'`; ip=`host $mx | sed '/IPv6/d;/handled/d' | awk '{ print $4 }'`; ... (8 Replies)
Discussion started by: Azrael
8 Replies

5. Programming

Passing multiple values from a function in C

I know multiple values can be returned from a function in C like this: char **read_file ( char * , unsigned long int * );//this is the function prototypeunsigned long int number_of_words = 0;//variable defined in main() and initialized to 0words_from_dictionary = read_file ( "dictionary.dit" ,... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. Programming

Function Returning Pointer

Hi guys. how a functions such fdopen, ... can return pointer? are these functions use static memory(variables)? (6 Replies)
Discussion started by: majid.merkava
6 Replies

7. Shell Programming and Scripting

how to capture oracle function returning 2 values in unix

i have an oracle function which returns two values, one is the error message if the function encounters anything and another one which returns a number i need to capture both and pass it on to unix shell script how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies

8. Shell Programming and Scripting

Returning the name of function used

Hi All In my script, I can call on several functions. I have a logging function that is called by any of these functions. What I would like is some way of identifying which function I am using and pass this to the log function as some parameter. Is there some built in command or way of... (3 Replies)
Discussion started by: kingpin2502
3 Replies

9. Shell Programming and Scripting

returning from a function

Hi all, I am very new to BASH shell programming. I need to return an integer from a function to the caller function. I did this: but it keeps giving me wrong return: Can someone help me out here, please? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

10. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies
Login or Register to Ask a Question