Thread: dynamic 2D array question

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    dynamic 2D array question

    With a dynamic 2D array:

    int *arr;

    arr = int *calloc(maxrow*maxcol, sizeof(int));

    how do you reference the different elements (alternate notation arr[row][col] will not compile). Is pointer arithmetic the solution?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    int **arr;
    arr = malloc( maxrow * sizeof(int*) );
    for ( i = 0 ; i < maxrow ; i++ ) arr[i] = calloc( maxcol, sizeof(int) );
    Then you can use arr[row][col] notation

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    18
    Awesome. Thanks Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  4. 2D Array spreadsheet question
    By cdonlan in forum C Programming
    Replies: 7
    Last Post: 01-24-2005, 08:22 AM
  5. Dynamic 2D arrays question
    By MadStrum! in forum C Programming
    Replies: 7
    Last Post: 02-08-2003, 05:54 AM