How to pass array element form a subroutine to main function

AI Thread Summary
The discussion revolves around transferring an array from a subroutine to the main function in Fortran using the common block technique. The user is encountering an issue where the array elements are returning constant values instead of the expected varying values. They have declared the array `bparz(1500)` in a common block but are unsure how to access it correctly in the main function. Participants suggest stripping down the code to isolate the problem, emphasizing the importance of providing a minimal, compilable example to facilitate troubleshooting. There is a mention of the complexity due to the program being modified by multiple programmers, which adds to the confusion regarding common block usage. Additionally, there are side discussions comparing Fortran with C/C++ regarding array handling and memory management, with suggestions to consider dynamic memory allocation instead of static arrays. The conversation highlights the need for clarity in code structure and proper common block declaration to resolve the issue effectively.
FZK
Messages
6
Reaction score
0
Hi guys.

I want to transfer each elements of an array (which is declared in one subroutine)to main function using common block technique.
When i am doing so i go always a constant value in the array in the main function.

Suppose i have one array bparz(i) which has different values in one subroutine for all the values of i, but when i want to transfer the whole array to main function using common block then it gives only constant value for all values of i.
So if anybody has some guidance then please guide me
 
Technology news on Phys.org
I guess you use Fortran?

Try to strip your program of everything that is not necessary and get to the point when you have only code that reproduces your problem.

In my experience you will either find a bug while removing other parts of the code, or you will end with just a few lines that you will be able to post here.
 
borek said:
i guess you use fortran?

Try to strip your program of everything that is not necessary and get to the point when you have only code that reproduces your problem.

In my experience you will either find a bug while removing other parts of the code, or you will end with just a few lines that you will be able to post here.

yes i am unsing fortran.
Please tell me in details...
If you want programme i can post it here...

Reply me
 
Remove all unnecessary functions, variables, declarations, everything that is not related to the problem - leave just enough code so that it can be compiled and reproduces the problem when is executed.
 
Last edited by a moderator:
Thanks for your nice reply...here i am sending the exact portion of my problem so please see that and comment on...
In this programme i decleared bparz(1500) in a named common block statement like
common/gammaz/bparz(0:1500)...
Now i want to use these common block array elememts in main function...
But i cannot able to transfer it
***********************************
do 503 z=0,nz
do 500 j=0,nsprd
do 501 nb=0,nephas
do 502 i=1,npart
gaz(z)=ga(i,j,nb)
bperz(z)=bper(i,j,nb)
bparz(z)=sqrt(1.-(bperz(z)*bperz(z))-1./(gaz(z)*gaz(z)))
502 continue
501 continue
500 continue
503 continue
****************************************
now tell me what should i do
 
This is not what I asked for. This is not code that can be compiled, this doesn't tell anything about how the common block is declared and accessed. Please read my earlier posts again.
 
Last edited by a moderator:
Thanks for your reply
actually programme was made by several programmers and i am also modify the same programme for my uses wher i want to use common block for transfering data from one subroutine to another subroutine. So please guide me how i can use it and acces it in another subroutine...please help me
 
Sorry, without further effort on your side and without further information nobody will be able to help.
 
Last edited by a moderator:
Yes i will try myself...but can you help me how i can decleared common block array and how to acces it?
 
  • #11
i don't knw abt fortran but...in C/C++ i do like this...

void abc()
{
static int arr[8];
// do ur processing and then call main..
main(arr);

}

int main(int *arr)
{
// here you can use that arr variable... but one little thing you need that array to b static remember...
now you can easily use it..
}
 
  • #12
Why static, if anything, it is better to allocate memory dynamically and pass just the pointer. But this is completely OT.
 
  • #13
hmm...
 
  • #14
Msid17 said:
i don't knw abt fortran but...in C/C++ i do like this...

void abc()
{
static int arr[8];
// do ur processing and then call main..
main(arr);

}

int main(int *arr)
{
// here you can use that arr variable... but one little thing you need that array to b static remember...
now you can easily use it..
}

Calling main and passing the array address is pretty unusual, since the main function is the usual entry point for an application in C and C++ .
 
  • #15
Yes, ur rite Mark... i mean but its completely possible ... i just gave the shortest solution...:P there are a thousand other ways you can do that..:)
 
Back
Top