dynamic 2D array question

This is a discussion on dynamic 2D array question within the C Programming forums, part of the General Programming Boards category; With a dynamic 2D array: int *arr; arr = int *calloc(maxrow*maxcol, sizeof(int)); how do you reference the different elements (alternate ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    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, 04:16 PM
  4. 2D Array spreadsheet question
    By cdonlan in forum C Programming
    Replies: 7
    Last Post: 01-24-2005, 07:22 AM
  5. Dynamic 2D arrays question
    By MadStrum! in forum C Programming
    Replies: 7
    Last Post: 02-08-2003, 04:54 AM

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