Thread: matrices with malloc

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    18

    matrices with malloc

    Hi, I want to know how to create a interger matrix of [n] rows and [m] columns with malloc.

    any ideas

    Code:
        *matix = malloc ( sizeof (int) * n );
        if ( matrix == NULL )
        exit ( EXIT_FAILURE ); /* Memory could not be allocated, so exit. */
    creates an array of length [n]
    I know I want to create an array [n] long of arrays [m] long but I really have no idea of how to do it...

    help me please

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    You already asked this question in another thread. Please be more patient next time.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Yer I did, sorry and thanks at the same time

    to clarify some questions are these equivalent

    *array (equiv too) array[]
    **matrix (equiv too) matrix[][]

    if so, I recon I understand it

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Shotgun
    Yer I did, sorry and thanks at the same time

    to clarify some questions are these equivalent

    *array (equiv too) array[]
    **matrix (equiv too) matrix[][]

    if so, I recon I understand it
    Yes. But just incase you don't know how to use the last one in pointer notation correctly, here's how:

    *((*(matrix + row)) + col) = 5;

    The parenthesis sets the precedence, starting from the inner most and left to right. Or, you could make it easy on yourself and just do:

    matrix[row][col] = 5;

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    This is my current code
    NOTE: totalCustomers is # of rows and totalProducts is # of columns

    Code:
        int **matrix;
    
        **matrix = malloc ( sizeof (int) * totalCustomers );
    
        for ( i=0; i < totalCustomers; i++ )
         {
           matrix[i] = malloc ( sizeof (int) * totalProducts );
         }
    I am getting a seg fault on the first malloc
    also when i compile i also get a warning
    assignment makes integer from pointer without a cast
    any ideas...
    Thanks

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Shotgun
    This is my current code
    NOTE: totalCustomers is # of rows and totalProducts is # of columns

    Code:
        int **matrix;
    
        **matrix = malloc ( sizeof (int) * totalCustomers );
    
        for ( i=0; i < totalCustomers; i++ )
         {
           matrix[i] = malloc ( sizeof (int) * totalProducts );
         }
    I am getting a seg fault on the first malloc
    also when i compile i also get a warning

    any ideas...
    Thanks
    What's in bold above is your problem. You don't double-dereference, you just use the matrix variable itself: matrix = malloc(....).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM