Hello,

I have a question, How I can write a function that return 2 values.
the function should take 3 numbers and do between the first and the second number an arethmetic operation and check if the result will be the third number, if yes the function return 1 and return the character of the arethmetic operation.
for example: the function take the numbers 3 5 15, the function will check the three numbers, and will fint that 3*5= 15, so it will return 1 and return *.

I hope that you understand me, and I hope that you can help me with the first thread :)

A common way of return multiple values is by returning a struct which has multiple members, or for the function to accept a pointer that receives additional data from the function.

You don't actually need to return multiple values though: return 0 if there is no operation, '*' if multiplication is the operation, '+' if addition, etc etc. '*', '+', '-', and '/' are non-zero characters with all character sets that I know of.

thank you grampier
but by the question that i have I should reurn 2 values if there is operation return 1 and the character, I asked somebody the all tell me that I should use the pointers but anyone of them know how should write the function.

You can't return two values from a functio using the return statement. Use a struct as you've been told to above:

typedef struct
{
int x, y;
} P;

P getPoint()
{
int a, b;
//a = .... b = .....
P temp;
temp.x = a;
temp.y = b;
return temp;
}

thanks minas
but I want use the poiner, I sure that I can use the pointer, but I didn't know how to use it !! :(
is anyone know ?

You need to put some effort into understanding the answers you receive, rather than giving up if you're not given code examples.

The approach with using pointers is;

SomeTypeA Function( <arguments>,   SomeTypeB *x)
{
       *x = whatever_data_needed_other_than_return_value();
       return return_value();
}

The "SomeTypeB *x" argument is what I was referring to when I said "or the function to accept a pointer that receives additional data from the function".

So 1 has to be returned for you to know that the result is the third number. You can do it with only one return value:

char arithmeticOperation(int a, int b, int c)
{
	// +
	if(a + b == c) return '+';

	// -
	if(a - b == c) return '-';

	//you also do this if the order you pass the numbers isn't critical
	if(b - a == c) return '-';

	// *
	if(a * b == c) return '*';

	// /
	if(a / b == c) return '/';
	if(b / a == c) return '/';

	// %
	if(a % b == c) return '%';
	if(b % a == c) return '%';

	//if nothing matches
	return ' '; // return a space
}

int main(void)
{
	char c = arithmeticOperation(4, 2, 0);

	if(c == ' ') // check if it's a space which means there isn't an operation
		printf("No arithmetic operation found.\n");
	else
		printf(%c, "Operation ", c, " found.\n");
}

thank you grumpier
but I didn't understand the example, if you can give me actual example plz.

minas thank you, but I said that we should write the function with pointers, and te function should return 2 values (the character and the number 1).

I can write it by the structer but we should write it just with pointers !!!

Do you want the function to take pointers as parameters or return a pointer or both?
And why do you want to return 1?

but I didn't understand the example, if you can give me actual example plz.

I can, but I won't. As horrifying as the concept clearly is, you need to apply some effort to understand the answers you've been given.

Ok, let's go step by step.
We will create a struct that has two members, a char and an int.
Go on and declare the struct.

grumpier
I didn't ask you to write the func that i want, but I want another example but with parameters to understand the way.
minas I have write the func with structer

#include<stdio.h>
struct mytype
{
int ok;
char ch;
};
 
struct mytype fundCalculation(int a,int b,int c)
{
struct mytype tmp;
if(c==a+b)  {tmp.ok=1;tmp.ch='+';}
else if(c==a-b) {tmp.ok=1;tmp.ch='-';}
else if(c==a*b) {tmp.ok=1;tmp.ch='*';}
else if(c==a/b) {tmp.ok=1;tmp.ch='/';}
else {tmp.ok=0;tmp.ch=' ';}
 
return tmp;
 
}
main()
{
 
 
printf("%i %c ",fundCalculation(3,4,12).ok,fundCalculation(3,4,12).ch);
 
getchar();      
}

Well it works, good job!

Some tips:
Use this form when declaring a struct.

typedef struct
{
int ok;
char ch;
} mytype;

So you don't have to prefix mytype with struct all the time. Now you can do this:

mytype fundCalculation(int a,int b,int c)
{
  /* ....... */
}

yes I know that it works, but in the question we should write the func just with pointers !!!

Then instead of ints in the argument list, use pointers to ints.

a pointer to int: int *a = 0;

when you call the function, you need to pass addresses.

so

int x = 4;
callSomeFuncTakingPointers(&x);

when you need to access a value stored in a pointer, you the * operator.
int x = 3;
int *p = &x;
printf(%d, *p);

If you want to learn more about pointers,
http://www.cplusplus.com/doc/tutorial/pointers.html

like this ?

int findCalculation(int *a, int *b, int *c)
{
	char ch;
	if (*a - *b == *c)
		return 1;
	else return 0;

	if (*a + *b == *c)
		return 1;
	else return 0;

	if (*a * *b == *c)
		return 1;
	else return 0;

	if (*a / *b == *c)
		return 1;
	else return 0;

}

Yes, but use our function that returns the struct.

How can I do this :( ?!!

#include<stdio.h>
struct mytype
{
int ok;
char ch;
};
 
struct mytype fundCalculation(int a,int b,int c)
{
struct mytype tmp;
if(c==a+b)  {tmp.ok=1;tmp.ch='+';}
else if(c==a-b) {tmp.ok=1;tmp.ch='-';}
else if(c==a*b) {tmp.ok=1;tmp.ch='*';}
else if(c==a/b) {tmp.ok=1;tmp.ch='/';}
else {tmp.ok=0;tmp.ch=' ';}
 
return tmp;
 
}
main()
{
 
 
printf("%i %c ",fundCalculation(3,4,12).ok,fundCalculation(3,4,12).ch);
 
getchar();      
}

Instead of passing ints, pass pointers to ints. you did it above; try it and show us, even if it doesn't compiles.

like this ??

#include <stdio.h>
struct mytype
{
int ok;
char ch;
} mytype;
 
struct mytype fundCalculation(int *a,int *b,int *c)
{
struct mytype tmp;
if(*c==*a+*b)  {tmp.ok=1;tmp.ch='+';}
else if(*c==*a-*b) {tmp.ok=1;tmp.ch='-';}
else if(*c==*a * *b) {tmp.ok=1;tmp.ch='*';}
else if(*c==*a / *b) {tmp.ok=1;tmp.ch='/';}
else {tmp.ok=0;tmp.ch=' ';}
 
return tmp;
 
}
main()
{
 
 
printf("%i %c ",fundCalculation(3,4,12).ok,fundCalculation(3,4,12).ch);
 
getchar();      
}

The function is correct, but you don't call it correctly.
Create 3 variables in main x, y, z, give them values and pass their address to the function.

like this ??

if yes, but we use the structer, and we should just use in the function the pointers without anything else ....

this is the code:

#include <stdio.h>
struct mytype
{
int ok;
char ch;
} mytype;
 
struct mytype fundCalculation(int *a,int *b,int *c)
{
struct mytype tmp;
if(*c==*a+*b)  {tmp.ok=1;tmp.ch='+';}
else if(*c==*a-*b) {tmp.ok=1;tmp.ch='-';}
else if(*c==*a * *b) {tmp.ok=1;tmp.ch='*';}
else if(*c==*a / *b) {tmp.ok=1;tmp.ch='/';}
else {tmp.ok=0;tmp.ch=' ';}
 
return tmp;
 
}
main()
{
 
int x=3;
int y=4;
int z=12;

printf("%i %c ",fundCalculation(&x,&y,&z).ok,fundCalculation(&x,&y,&z).ch);
 
getchar();      
}

Yes.

In main, it's a better idea to actually create an oject
int main()
{
struct mytype mt = fundCalculation(....)

thank you very much minas1 :)

thanks again for helping and tips :icon_wink:

Nothing. Now show us the complete program. also use the typedef for the struct like I told you.

Ok, I will.
but the program is very long. contain some function, when I finish it I will put it here :)

I think what you want to do is just return the one or zero rather than a struct. The operation can be passed as a pointer to a char (in the parameters). a, b, and c don't need to be pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.