How to pass array element form a subroutine to main function

Click For Summary

Discussion Overview

The discussion revolves around the challenge of transferring elements of an array declared in a subroutine to the main function using the common block technique in Fortran. Participants explore issues related to accessing and manipulating array values across different scopes within the program.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their attempt to transfer an array `bparz(i)` using a common block but encounters a problem where the main function receives constant values instead of the expected varying values.
  • Several participants suggest simplifying the code to isolate the problem, with one emphasizing the importance of providing a minimal, compilable example to facilitate troubleshooting.
  • Another participant expresses a desire for guidance on how to declare and access common block arrays across subroutines, indicating a lack of familiarity with Fortran.
  • A participant unfamiliar with Fortran shares a C/C++ approach for passing arrays, suggesting the use of static arrays and calling the main function, which prompts a discussion about the appropriateness of this method.
  • There is a brief exchange regarding memory allocation strategies, with one participant advocating for dynamic memory allocation over static arrays, although this is noted as off-topic.

Areas of Agreement / Disagreement

Participants generally agree on the need for clearer code examples and the importance of isolating the problem. However, there are differing opinions on the best practices for array handling and memory management across programming languages, particularly between Fortran and C/C++.

Contextual Notes

Limitations include the lack of specific details about how the common block is declared and accessed, as well as the absence of a complete, compilable code example that reproduces the issue described.

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..:)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
20
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
235
Views
15K
  • · Replies 1 ·
Replies
1
Views
3K