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

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As I said earlier, there is no way you can do that. NOT ANY WAY!

    [if anyone thinks that _msize() is the right solution, may I just point out that we don't even know what compiler the OP is using, and it's definitely and absolutely not portable to any other compiler or even another C library].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bithub View Post
    Now you're just trolling us... right?
    Hanlon's Razor: Never attribute to malice that which can be adequately explained by stupidity.


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

  3. #18
    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?

  4. #19
    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.

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Agreed! but why create unnecessary confusion for others? Let's say COLUMN_SIZE is 4 and ROW_SIZE is 5. So, in general we would say he wants to create a 5 *4 matrix. But that above code would create a 4 *5 matrix. Total elements are definitely same(20). But that does not mean he is right!

    Later on if he wants to process it and keeps the outer loop as ROW_SIZE and inner as COLUM_SIZE what would happen at that time?the compiler is bound to give some garbage values And ya now don't argue that what if he keeps the outer loop as COLUMN_SIZE and inner as ROW_SIZE. Wrong is wrong and that is not going to be true if you keep arguing. He or others are probably not going to process array each and every time in "Transpose manner". "Names are confusing and THEY ARE".

    -Chotti.

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It really doesn't make any difference - it's just a matter of convention. Hell, if you look at a lot of matrix libraries out there you'd find that they use a column-major layout just as often as not. As Quzah said, as long as you are consistent about it you won't have any problems.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #22
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    If you read every other post by this guy, you'll notice that he has never once taken advice or even apparently read a single helpful post. Just stop wating your time.

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