Sponsored Content
Top Forums Programming returning multiple values from a function in C Post 302278061 by steephen on Monday 19th of January 2009 07:25:51 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
STRCPY(3)						   BSD Library Functions Manual 						 STRCPY(3)

NAME
stpcpy, strcpy, strncpy -- copy strings LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <string.h> char * stpcpy(char *s1, const char *s2); char * strcpy(char *restrict s1, const char *restrict s2); char * strncpy(char *restrict s1, const char *restrict s2, size_t n); DESCRIPTION
The stpcpy() and strcpy() functions copy the string s2 to s1 (including the terminating '' character). The strncpy() function copies at most n characters from s2 into s1. If s2 is less than n characters long, the remainder of s1 is filled with '' characters. Otherwise, s1 is not terminated. The source and destination strings should not overlap, as the behavior is undefined. RETURN VALUES
The strcpy() and strncpy() functions return s1. The stpcpy() function returns a pointer to the terminating '' character of s1. EXAMPLES
The following sets chararray to ``abc'': char chararray[6]; (void)strncpy(chararray, "abc", sizeof(chararray)); The following sets chararray to ``abcdef'': char chararray[6]; (void)strncpy(chararray, "abcdefgh", sizeof(chararray)); Note that it does not NUL terminate chararray, because the length of the source string is greater than or equal to the length argument. The following copies as many characters from input to buf as will fit and NUL terminates the result. Because strncpy() does not guarantee to NUL terminate the string itself, this must be done explicitly. char buf[1024]; (void)strncpy(buf, input, sizeof(buf) - 1); buf[sizeof(buf) - 1] = ''; This could be better achieved using strlcpy(3), as shown in the following example: (void)strlcpy(buf, input, sizeof(buf)); Note that, because strlcpy(3) is not defined in any standards, it should only be used when portability is not a concern. SECURITY CONSIDERATIONS
The strcpy() function is easily misused in a manner which enables malicious users to arbitrarily change a running program's functionality through a buffer overflow attack. (See the FSA and EXAMPLES.) SEE ALSO
bcopy(3), memccpy(3), memcpy(3), memmove(3), strlcpy(3) STANDARDS
The strcpy() and strncpy() functions conform to ISO/IEC 9899:1990 (``ISO C90''). The stpcpy() function is an MS-DOS and GNUism. The stpcpy() function conforms to no standard. HISTORY
The stpcpy() function first appeared in FreeBSD 4.4, coming from 1998-vintage Linux. BSD
August 9, 2001 BSD
All times are GMT -4. The time now is 09:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy