# Arrays in Fortran & C

 User Name Remember Me Password
 Register Blogs Members List Search Today's Posts Mark Forums Read

 December 11, 2004, 09:44 Arrays in Fortran & C #1 SwF Guest   Posts: n/a I got accustomed that in Fortran A(i) is i-th element of the array A, but in C/C++ A[i] is (i+1)-th element of the array. Is it possible to do (to work) with indexes of arrays in C/C++ as in Fortran?

 December 11, 2004, 20:24 Re: Arrays in Fortran & C #2 zxaar Guest   Posts: n/a you probaly want to say that in C/C++ arrays A[i] is i - 1 th element , ..... since arrays in C/C++ start at element 0, that is array with size 5 is 0 1 2 3 4 (from element index) ..... and in the end .... in all the computer languages ... A[i] means ith element .. only in C if you try to access A[array-size] it will give you an exception ....

 December 11, 2004, 20:52 Re: Arrays in Fortran & C #3 andy Guest   Posts: n/a Yes. You can overload ( and ) in C++ but why bother? But more importantly in C/C++ arrays are not arrays in the Fortran sense but only pointers. This defeats the optimiser (aliasing) and leads to lower performance.

 December 12, 2004, 03:04 Re: Arrays in Fortran & C #4 SwF Guest   Posts: n/a To zxaar: let i=0, on your logic A[0] is 0-1=-1-th element of the array (minus one element, i.e. nonsense). In fact: A[0] is 0+1=1-th element (the first element of the array). Is not it?

 December 12, 2004, 03:37 Re: Arrays in Fortran & C #5 zxaar Guest   Posts: n/a but by your logic ... let i = 0 , then if u ask for A[0] computer shall return you A[0+1] or A[1] ...so please read first what you have written ... then we will think on other logic ...

 December 12, 2004, 04:21 Re: Arrays in Fortran & C #6 SwF Guest   Posts: n/a I use logic accepted in Fortran and in usual life: a[0] is the first element, a[1] is the second, etc. Suppose I have a piece of code in Fortran: real*8 a(1:100),b(2:99) do i=2,99 b(i)= a(i-1) - 2.0*a(i) + a(i+1) end do Please write this piece in C/C++.

 December 13, 2004, 12:06 Re: Arrays in Fortran & C #7 andy Guest   Posts: n/a I think I misunderstood your question first time. You cannot do it in C but you can in C++ if you want to make the effort. Here is an example: http://math.nist.gov/tnt/ of some relatively straightforward to understand C++ (unlike most). It will, of course, take ages to compile, require you to use the same version of the compiler for all code and be slower than Fortran but it can be done.

 December 13, 2004, 13:45 Re: Arrays in Fortran & C #8 Praveen Guest   Posts: n/a If you want an array of size N just allocate for N+1. Then use the indices from i=1 to i=N as in fortran. real a[N+1]; for(i=1; i<=N; i++) use a[i] for some computation You will be wasting a[0] but no big deal.

 December 13, 2004, 15:25 Re: Arrays in Fortran & C #9 D.M. Lipinski Guest   Posts: n/a SwF, If you declare A as a ponter in C/C++ (i.e. double *A) , then allocate the required N elements with calloc/malloc. Now, you can pass A-1 instead of A into your C/C++ function. By doing this you pass just a fictitious address (one element less than the address of A) and, within the function, you can access the first element of A as A[1], i.e. fortran-like. Hope it solves your problems regards DML

 December 14, 2004, 11:01 Re: Arrays in Fortran & C #10 Amadou Sowe Guest   Posts: n/a I think this might be useful in attempting to do what Lipinski suggested: #include #include #include #define N 101 int main() { double *a, *b, pi; int i; /* Allocate memory for vector a so index manipulation instead can be done */ if((a=(double *) calloc(N, sizeof(double)))==(double *) NULL){ perror("calloc:a:"); /* Gives error message if memory not allocated */ exit(1); } /* Allocate memory for vector b so index manipulation instead can be done */ if((b=(double *) calloc(N, sizeof(double)))==(double *) NULL){ perror("calloc:b:");/* Gives error message if memory not allocated */ exit(1); } pi=4.0*atan(1.0); for(i=1;i

 December 14, 2004, 11:55 Re: Arrays in Fortran & C #11 Amadou Sowe Guest   Posts: n/a SwF, I tried pasting the example in but it did not come out right. The print statements are some how missing. I can send it to you if you give me an e-mail to send it to.

 January 14, 2005, 08:43 Re: Arrays in Fortran & C #12 Alexander Starostin Guest   Posts: n/a If you've got inevitably accustomed. Always create in C the arrays with plus one extra element.

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Araz CFX 12 November 2, 2014 13:46 hawk Main CFD Forum 1 April 12, 2005 22:13 Monica Main CFD Forum 1 August 28, 2004 20:45 Swapnil CFX 2 November 26, 2002 16:16 Zi-Wei Chiou Main CFD Forum 35 September 26, 2001 09:34

All times are GMT -4. The time now is 15:08.

 Contact Us - CFD Online - Top