i have a function that will need to return 2 varibles, a int and a char. how i could i assigned 2 varibles from this function?
thanks
i have a function that will need to return 2 varibles, a int and a char. how i could i assigned 2 varibles from this function?
thanks
if your function needs to return 2 values you probably have a design flaw.
You have several choices. Best is to pass your variables that need returning into function by reference i.e. as a pointer.
alternatively pass address of a struct into function and store your 2 return values in struct.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
i need to return 2 varibles for a arguement check. if the user doesnt eneter any on the command-line, they get a "shell" function from my program. i another function(next_word) would copy the string until theres a space for the arg handler. but i need to 2 vaules straight.
my function get_arg see if the first 1 arg is a flag, if it is it copy the 2nd arg to a varible that gets used near the end. It return 1 on error. But most of the time the program will only be ran with 1 arg, a filename. So i was thinking pass one arg to it and if it gives a error do it again with 2 args. Most of time it will only have one so this should be that bad.Code:while(prompt[x] != '\0') { get_arg( next_word(prompt, x), '/0', 1) switch( get_arg( next_word(prompt, x), '/0', 1) ) { /* some stuff here*/ } }
Any idea on how to do this or a better way?
A function can at most return one value. If you need to pass two or more values back use pointers and pass by reference.
Mr. C.
how? i dont see how that is doing anything but changing types.
can any explain this?
maybe if you were a bit clearer about what you are trying to achieve and bumping of threads here is viewed badly. punishments have included thread deletion and turning all your threads pink. You've been warned!
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Using pointers to get values can be done like in the example.
Code:/* Function to get values */ void function (int *a, int *b) { ... } int main (void) { int a, b; ... /* Get values of a and b */ function (&a, &b); /* Check values of a and b */ check_val (a, b); return 0; }
maybe be this is a better question, how would you handle taking command line arguemnts(like -t DATA) in a program(not from the command line)? I had a cheezy idea in mind but it doesnt seem like its a good idea(it doesnt even work actually). Anyone have some code from a program I can see?
and thanks for the info Stoned_Coder!
Use a struct with all of the variables you need to return and then just pass an instance of the struct. That way you only return one object, but a bunch of variables get changed. It also makes your function tag simpler.
BebopCode:typedef struct { char flag; char *value; } CMD_LINE; CMD_LINE get_args( char *argv[], CMD_LINE args ) { /* Just faking what the function does. */ if( argv[1][0] == '-' && argv[1][1] == 'f' ) { args.flag = 'f'; args.value = "Format something"; } return args; }
Processing error: Stupidity detected.
------------------------------
Dr. Bebop
Windows XP Professional Ed.
Microsoft Visual Studio 6