|
[Sponsors] |
May 5, 2007, 10:08 |
what is the meaning of the following c code
|
#1 |
Guest
Posts: n/a
|
Hi The code are as following:
" double **matrix; matrix = (double **) malloc (N * sizeof (double)); for (row = 0; row < N; row++) matrix[row] = (double *) malloc (N * sizeof (double));" i do not understand these four lines , would you please detail it. Regards |
|
May 5, 2007, 10:49 |
Re: what is the meaning of the following c code
|
#2 |
Guest
Posts: n/a
|
it is the usual way in C to create a 2D matrix A. you can access to its element like matlab simply requiring: A[i][j]. Luca
|
|
May 5, 2007, 11:41 |
Re: what is the meaning of the following c code
|
#3 |
Guest
Posts: n/a
|
Suppose you say double *A. This means A will hold the address of a variable. You will allocate memory like: A=(double *)malloc(sizeof(double)*N); and *A will give the value at the first address. Then subsequent values can be obtained by *(A+1), *(A+2), etc....
Now lets say double **A. This tells *A will hold the address of a variable. You will allocate memory like: A = (double **)malloc(size(double)*N); Actually u are allocating memory for N rows only. The column part hasnt been allocated yet. So For each Row of A, that loop you mentioned will allocate for all N columns: for (row = 0; row < N; row++) A[row] = (double *) malloc (N * sizeof (double)); The above will allocate space for N columns when row=0,1,2...N etc. So ultimately at the end you wud have allocated memory for a NxN matrix. To find the value at the ith row and jth column of the matrix, just issue, *(*(A+i) + j). -Dominic |
|
May 5, 2007, 12:17 |
Re: what is the meaning of the following c code
|
#4 |
Guest
Posts: n/a
|
Thank you all for your excellent explaination.
|
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
The FOAM Documentation Project - SHUT-DOWN | holger_marschall | OpenFOAM | 242 | March 7, 2013 13:30 |
Debugging Unsteady 2-D Panel Method Code: Wake Modeling | RajeshAero | Main CFD Forum | 5 | November 10, 2011 06:48 |
Open Source Vs Commercial Software | MechE | OpenFOAM | 28 | May 16, 2011 12:02 |
Design Integration with CFD? | John C. Chien | Main CFD Forum | 19 | May 17, 2001 16:56 |
public CFD Code development | Heinz Wilkening | Main CFD Forum | 38 | March 5, 1999 12:44 |