hi, how i return field from function, which has an argument a field?
for example:
word f(word a[5]) {
word b[5];
for(int i= 0; i<5; i++) b[i] = -a[i];
return b[5]; //is this right?
}
or should i do it like this?
word *f(word a[5]) {
word b[5];
for(int i= 0; i<5; i++) b[i] = -a[i];
return *b;
}
and when i want to call function f, how can i do that? is called by another function, which takes a field as an argument. thanks.
|
Go4Expert Member
|
|
| 6Nov2009,09:59 | #2 |
|
Code:
f(word* a,word** b){
int i=0;
for(;i<5;i++)
{
*b[i]=-a[i];
}
}
|
|
Mentor
|
![]() |
| 7Nov2009,00:11 | #3 |
|
If you want to return the whole array, you will have to create it on the heap (malloc) and remember somewhere to free it. It could be easier to pass the array in:
Code:
void test3a(int a[5], int b[5])
{
for (int i=0; i<5; i++)
b[i]=-a[i];
}
void test3()
{
int a[5],b[5];
for (int i=0; i<5; i++)
a[i]=i*2;
test3a(a,b);
for (int i=0; i<5; i++)
printf("%d ",b[i]);
printf("\n");
}
|
|
Newbie Member
|
|
| 7Nov2009,02:57 | #4 |
|
thanks both of you. I got it now.
|

