double arrays

This is a discussion on double arrays within the C Programming forums, part of the General Programming Boards category; I need an array of dimension NxN but I need to malloc instead of doing 'int a[N][N];' and read in ...

  1. #1
    Unregistered
    Guest

    double arrays

    I need an array of dimension NxN but I need to malloc instead of doing 'int a[N][N];' and read in integers. I tried the following:

    int ..........;
    ...
    a=(int **)malloc(N*sizeof(int**));
    and (a+i+j) to store the scanned value using nested for loops. It's not working since a+1+0 is the same as a+0+1

    could anyone help? C or C++
    Thanks,

    xer
    *new programer*

  2. #2
    Unregistered
    Guest
    first the proper declaration is
    int **a;
    first you have to allocate space so that a can hold N pointers
    a= mallco(N*sizeof(int *));
    the loop through i to allocate space for N elements in each of of the N pointers
    for(i=0;i<N;i++)
    a[i]=malloc(N*sizeof(int));


    a[i] or *(a+i) is the pointer to the ith row
    so the i,j element is *(*(a+i)+j) or a[i][j] or *(a[i]+j),

  3. #3
    Unregistered
    Guest
    thanks, I just figured out how to do it.

    xer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. needs help copying arrays
    By RedRattler in forum C Programming
    Replies: 3
    Last Post: 04-04-2003, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21