Scanf confusion, 2 dimensional array modification

This is a discussion on Scanf confusion, 2 dimensional array modification within the C Programming forums, part of the General Programming Boards category; So a dynamic two-dimensional array using malloc would be this: Code: int **two_d_array; two_d_array = malloc(rows * sizeof(int *)); for ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So a dynamic two-dimensional array using malloc would be this:
    Code:
    int **two_d_array;
    two_d_array = malloc(rows * sizeof(int *));
    for (int i = 0; i < rows; i++) {
        two_d_array[i] = malloc(columns * sizeof(int));
    }
    return two_d_array;
    I think I have all the keywords, so that should show up on a search. Later you will have to do this:
    Code:
    for (int i = 0; i < rows; i++) {
        free(two_d_array[i]);
    }
    free(two_d_array);
    So make sure you keep hold of the pointer.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok, what you have said seems to be working. but I don't know why x** and what the function returns are incompatible? I've declared the function to return an int**.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    int **x and the function are compatible. It's matrix[3][3] that isn't.

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok. To make this absolutely clear.
    How can I take what the function returns, and assign it to a two dimensional array??

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No. You can assign it to a double-star. Arrays (of any kind) are non-assignable at all times. That is,
    Code:
    int a[5];
    a = //there's nothing you can put here that would make it right

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    So why we are talking about function that return pointer to arrays since we cannot assign them to arrays.??
    And if you have a way to do that in mind, please suggest it. I hope you do realize the time difference between us.
    Thanks,

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Leojeen View Post
    So why we are talking about function that return pointer to arrays since we cannot assign them to arrays.??
    And if you have a way to do that in mind, please suggest it. I hope you do realize the time difference between us.
    Thanks,
    So this whole boondoggle requires you to completely forget that arrays exist. Everything and I mean everything must be int**. Once you write int matrix[4][4], you're sunk. If you consistently use double-stars, you can assign double-stars to your heart's content.

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok, so it worked after declaring matrix itself as an int**. I got your point finally. Thank you for u time!!

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Incidentally, there is another way to do this: create a single huge (dynamically allocated) array, and then use it as a two dimensional array by computing offsets yourself. The good part is that you only malloc() and free() once and get contiguously allocated space; the bad part is that you have to compute the offsets yourself and the chance of there being insufficient space left to allocate is higher since the space must be allocated contiguously.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  2. Replies: 1
    Last Post: 04-25-2006, 01:14 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM

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