Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-05 Thread Vishal Thanki
Hi Gene, I was thinking the same thing which you implemented in your first snippet. But I tried it without using typedef. Following is my code: #include stdio.h #include malloc.h int main() { int (*ptr)[4] ; ptr = (int ((*)[4]))malloc(2*sizeof(int (*)[4])); if (ptr) {

[algogeeks] Re: 2 D array(dynamic allocation)

2011-07-02 Thread Sandeep Jain
Here's my solution. int** allocateMatrix(int m, int n) { int **rowList = (int**)malloc(sizeof(int)*m*n + sizeof(int*)*m); int *colList = (int*)(rowList+m); int i; for(i=0; im; i++) { rowList[i] = colList+i*n; } return rowList; } And here's the main method to test/understand the

Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-02 Thread vaibhav shukla
@sandeep sir: thnx... good 1 :) On Sat, Jul 2, 2011 at 11:32 PM, Sandeep Jain sandeep6...@gmail.com wrote: Here's my solution. int** allocateMatrix(int m, int n) { int **rowList = (int**)malloc(sizeof(int)*m*n + sizeof(int*)*m); int *colList = (int*)(rowList+m); int i; for(i=0; im;

[algogeeks] Re: 2 D array(dynamic allocation)

2011-07-02 Thread Gene
Unfortunately this invokes undefined behavior, so it may not work for all architectures and compilers. It relies on pointers to int having the same alignment as int. By far the best way to do this is use C99 features: double (*a)[n_cols] = calloc(n_rows, sizeof *a); If you don't have C99 and

Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-01 Thread Anika Jain
do we need to add m in each a[i]?? shudnt it be a[i] = a+ n*i?? i think i m doing a silly mistake.. bt plz tell me.. On Thu, Jun 30, 2011 at 7:41 AM, Dave dave_and_da...@juno.com wrote: @Rizwan: Not completely. What if ROW in your code is a variable (not a constant) that is not known until run

Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-01 Thread sourabh jakhar
#includestdio.h int main() { int i,j; int ct=0; int *a[3]; for( i=0;i3;i++) { a[i]=(int **)malloc(sizeof(int)*4); for(i=0;i3;i++){ for(j=0;j4;j++) a[i][j]=ct++; printf(%d,a[i][j]); } } } On Sat, Jul 2, 2011 at 12:13 AM, Anika Jain anika.jai...@gmail.com wrote: do we need to add m in each a[i]??

[algogeeks] Re: 2 D array(dynamic allocation)

2011-07-01 Thread sravanreddy001
@saurabh.. No man.. it involves variable number of calls.. even if its present only once in code. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit

Re: [algogeeks] Re: 2 D array(dynamic allocation)

2011-07-01 Thread sourabh jakhar
#includestdio.h int main() { int i,j; int ct=0; int *a[3]; for( i=0;i3;i++) { a[i]=(int **)malloc(sizeof(int)*4); } for(i=0;i3;i++){ for(j=0;j4;j++) a[i][j]=ct++; printf(%d,a[i][j]); } On Sat, Jul 2, 2011 at 2:27 AM, sravanreddy001 sravanreddy...@gmail.comwrote: @saurabh.. No man.. it involves

[algogeeks] Re: 2 D array(dynamic allocation)

2011-06-29 Thread Dave
Apoorve: No. Dave On Jun 29, 2:34 pm, Apoorve Mohan apoorvemo...@gmail.com wrote: @piyush: only one call to malloc...ur sol has 2 On Thu, Jun 30, 2011 at 12:58 AM, Piyush Sinha ecstasy.piy...@gmail.comwrote: int **p; p = (int **)malloc(sizeof(int *)*row); for(i = 0;irow;i++)    

[algogeeks] Re: 2 D array(dynamic allocation)

2011-06-29 Thread Dave
@Rizwan: Not completely. What if ROW in your code is a variable (not a constant) that is not known until run time? Then you need to dynamically allocate space for your arr2D as well. So, contradicting my earlier No response, maybe something like this would work to allocate an array a with m rows