Reputation: 43
#include <stdio.h>
float orizousa(int n[]);
int main() {
int a[12];
int i;
printf("grapse tous sintelestes kai to apotelesma tis prwtis eksiswsis \n (me seira:1os-2os-3os sintelestis - apotelesma) :");
for(i=0; i<=11; i++) {
scanf(" %d",&a[i]);
}
float g = orizousa(a);
printf(" %f",g);
return 0;
}
float orizousa(int a[]) {
float d,dx1,dx2,dx3,x1,x2,x3;
d = a[0]*a[4]*a[8] + a[1]*a[5]*a[6] + a[2]*a[3]*a[7] - a[2]*a[4]*a[7] - a[1]*a[3]*a[8] - a[0]*a[5]*a[7] ;
dx1 = a[9]*a[4]*a[8] + a[1]*a[5]*a[11] + a[2]*a[10]*a[7] - a[2]*a[4]*a[7] - a[1]*a[10]*a[8] - a[9]*a[5]*a[7] ;
dx2 = a[0]*a[10]*a[8] + a[0]*a[5]*a[6] + a[2]*a[3]*a[11] - a[2]*a[10]*a[11] - a[1]*a[3]*a[8] - a[0]*a[5]*a[11] ;
dx3 = a[0]*a[4]*a[11] + a[1]*a[10]*a[6] + a[9]*a[3]*a[7] - a[9]*a[4]*a[7] - a[1]*a[3]*a[11] - a[0]*a[10]*a[7] ;
return d;
}
In this code, in the line that says
float g = orizousa(a);
why do we put only a
in, and not a[]
for example??
How can I return 2 values, for example d
and dx
, from one function??
Upvotes: 0
Views: 94
Reputation: 3061
why we put only 'a' in, and not a[] for example??
When passing array as arguments to functions in C, we have to pass the variable name only, unless you want to pass only one element of the array, which in that case would be float g = orizousa(a[2]) for example. But the function orizousa receives an array, so you have to pass the entire array.
and how can i return 2 values??
There is no way to return 2 values in C from a function. But what you can do is pass variables as reference. You can do something like this:
#include <stdio.h>
float orizousa(int n[]);
int main()
{
int a[12];
int i;
printf("grapse tous sintelestes kai to apotelesma tis prwtis eksiswsis \n (me seira:1os-2os-3os sintelestis - apotelesma) :");`enter code here`
for(i=0;i<=11;i++)
{ scanf(" %d",&a[i]);}
float d, dx1, dx2, dx3;
float g=orizousa(a, &d, &dx1, &dx2, &dx3);
printf(" %f",g);
return 0;
}
float orizousa(int a[], float *d, float *dx1, float *dx2, float *dx3)
{
*d=a[0]*a[4]*a[8] + a[1]*a[5]*a[6] + a[2]*a[3]*a[7] - a[2]*a[4]*a[7] - a[1]*a[3]*a[8] - a[0]*a[5]*a[7] ;
*dx1=a[9]*a[4]*a[8] + a[1]*a[5]*a[11] + a[2]*a[10]*a[7] - a[2]*a[4]*a[7] - a[1]*a[10]*a[8] - a[9]*a[5]*a[7] ;
*dx2=a[0]*a[10]*a[8] + a[0]*a[5]*a[6] + a[2]*a[3]*a[11] - a[2]*a[10]*a[11] - a[1]*a[3]*a[8] - a[0]*a[5]*a[11] ;
*dx3=a[0]*a[4]*a[11] + a[1]*a[10]*a[6] + a[9]*a[3]*a[7] - a[9]*a[4]*a[7] - a[1]*a[3]*a[11] - a[0]*a[10]*a[7] ;
}
Upvotes: 1
Reputation: 1266
float g=orizousa(a);
int a[]
a is an array of integer type where the variable name 'a' denotes the reference of array a.
function orizousa takes a reference parameter of integer type. thats why you have to pass a instead of a[]
if you want to return d and dx then simply add another reference to your function.
orizousa(int a[],float pReturn[])
{
....
pReturn[0]=d;
pReturn[1]=dx;
}
Upvotes: 1
Reputation: 36082
Arrays and pointers are intimately related. When you write "g=orizousa(a)" you are passing the function a copy of the address of the first element of the array a.
To return two values from a function you can either pass the address of the two variables you want to change or put them into a structure and return the structure.
void foo( int* x, int* y ) {
*x = 1; *y = 2;
}
called as
int xx = 0;
int yy = 0;
foo(&xx,&yy);
or as a struct
typedef struct {
int x;
int y;
} args;
either pass in as argument
void foo(args* arg) {
args.x=1;
args.y=2;
}
called as
args arg;
foo(&arg);
or returned
args foo() {
args arg = { 1,2 };
return arg;
}
called as
args arg = foo();
Upvotes: 1
Reputation: 6753
When you are passing an array to the function orizousa
, you are passing the address of the array, not an actual copy (atleast in this case). In C, a
actually stores the address of the first element of the array. So, you are passing the address of the first element of the array. *a
will print the value of the first element. we cannot pass a[]
because that is a syntax error.
To return 2 values, you can declare a variable in the main function and pass it as a reference variable to orizousa
. Then you can return d
and store the value of dx1 in the reference variable.
Upvotes: 0
Reputation: 1420
when it comes to array just the name, passes the array's address and the function accepting it will be pointing to the same memory address. it more like reference variable in C++.
u cant return more than one value at a time. if the data is of similar type u can copy to an array and pass the array back
Upvotes: 1