Thread: how to scan an int dinamic 2d array..

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by bithub View Post
    Here is how to do it for dynamic arrays:
    Code:
    int** a;
    a = malloc(COLUMN_SIZE * sizeof(int*));
    for(i = 0; i < COLUMN_SIZE; i++)
        a[i] = malloc(ROW_SIZE * sizeof(int));
    
    printf("column size is: %d\n", COLUMN_SIZE);
    printf("row size is: %d\n", ROW_SIZE);
    See what I did there?
    Aren't the ROW_SIZE and COLUMN_SIZE exchanged? i mean in 2D arrays first memory is allocated for rows and then only for columns?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by chottachatri View Post
    Aren't the ROW_SIZE and COLUMN_SIZE exchanged? i mean in 2D arrays first memory is allocated for rows and then only for columns?
    Typically, yes. It doesn't actually matter what you call it, just as long as you know what it is you're doing, and keep doing it that way. But yes, you generally think of it as:
    Code:
    allocate pointers for rows 
    for each row
        allocate one row (which is really just the number of columns for that row)
    But it doesn't actually matter how you think of it, just as long as you are consistent in your usage of X and Y.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM