Newbie Function question 
Author Message
 Newbie Function question

Im trying to jump from the perl world and have started teaching myself C.  I'm running on a Gnu-Linux box and using GCC and would like to keep the code as portable as possible.   My question relates to functions, and I hope is a simple one.  If I understand correctly, a function is defined as such...

int name(int x)

so we have the return type, function name and then any data you want passed to the function.  My question then, is can a
function return more then one data type?  For example, say I have a function that is doing my database talk for me and I want it to return a field entry, which is a string, and a value that would be say, just a type int.   How can I return those types seperatly from one function without having to regex the hell out of a string, or can I?

                 Jason
         www.cyborgworkshop.com
...and the geek shall inherit the earth...



Sun, 31 Oct 2004 09:08:02 GMT  
 Newbie Function question

Quote:
> Im trying to jump from the perl world and have started
> teaching myself C.  I'm running on a Gnu-Linux box and using
> GCC and would like to keep the code as portable as possible.
> My question relates to functions, and I hope is a simple one.
> If I understand correctly, a function is defined as such...

> int name(int x)

> so we have the return type, function name and then any data
> you want passed to the function.  My question then, is can a
> function return more then one data type?  For example, say
> I have a function that is doing my database talk for me and
> I want it to return a field entry, which is a string, and a
> value that would be say, just a type int.  How can I return
> those types seperatly from one function without having to
> regex the hell out of a string, or can I?

A function can not return more than "one scalar" (using
Perl-talk), which it really can't do in Perl either BTW.  If you
want to return a structure, define a struct that contains all
the data that you want to return and then return an object of
that structure type.

typedef struct {
    int anint;
    double some, doubles;
    char str[40];

Quote:
} mystruct;

mystruct myfunc(int someval) {
    mystruct thestruct;

    strcpy(thestruct.str "Gedan daginn");

    /* bladibladibla */

    return thestruct;

Quote:
}

--
Andreas K?h?ri
--------------------------------------------------------------
Feed your daemons.              www.netbsd.org


Sun, 31 Oct 2004 09:26:46 GMT  
 Newbie Function question
Hi,

Quote:
> > My question then, is can a
> > function return more then one data type?  For example, say
> > I have a function that is doing my database talk for me and
> > I want it to return a field entry, which is a string, and a
> > value that would be say, just a type int.  How can I return
> > those types seperatly from one function without having to
> > regex the hell out of a string, or can I?

Returning a string isn't as easy as it is in Perl.  The only thing you can
return is a pointer to a string (or, using Perl-talk again, a reference,
which is not entirely the same).  The memory required to store the string
also has to be allocated somewhere in your program (remember to allocate
enough space for the string including the termination 0).

Quote:
> A function can not return more than "one scalar" (using
> Perl-talk), which it really can't do in Perl either BTW.  If you
> want to return a structure, define a struct that contains all
> the data that you want to return and then return an object of
> that structure type.

A different solution is to pass storage pointers with the function call,
e.g.:

    int myfunc(int in, int* out);

myfunc can store information (in this case of type int) at the location the
out pointer points to.  Of course you can pass as many as you want of these
(as long as you prototype your function as such).

A third solution, which noone would recommend I hope, is using global
variables.  But pretend you never read that last line.

Good luck with it,

John



Sun, 31 Oct 2004 14:35:55 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. newbie question: returning structs from functions

2. Newbie question about function to get float

3. Newbie Question: select function

4. Newbie question: malloc() function

5. another newbie question: strncpy function

6. Newbie question: Pointer to an array and passing it into a function

7. Newbie question: Calling functions

8. Newbie question about fgets() Function

9. newbie question: exporting a string from a function using vc++4

10. newbie question: passing struct as argument to function ?

11. Newbie question: Returning LPSTR from a function

12. Newbie question about printing text and timer function

 

 
Powered by phpBB® Forum Software