Thread: CALLOC / 2-D Array Creation

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    CALLOC / 2-D Array Creation

    I have an assignment to create a two dimensional array based on user inputs. A prompt is done for the size of the row and column and then a 2-D array is to be created based on the inputed dimensions. The attached code compiles okay but crashes if the row and column are not the same size. For example, if I input row = 10, col= 10 then everything is okay but if I input row = 5, col = 6, then the program crashes.

    Suggestions?

    [code]
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h> /* Required for random integer */
    #include <windows.h> /* Required for clearScreen function */

    int main (void) {

    short int outer;
    short int inner;
    short int rowInput;
    short int colInput;
    short int size;
    short int *row;
    short int *col;

    int **arrayx;

    printf("Enter the row: ");
    scanf("%d", &rowInput);

    printf("\nEnter the col: ");
    scanf("%d", &colInput);

    row = &rowInput;
    col = &colInput;

    size = *row * *col;

    printf("\n\nSize is %d", size);
    printf("\n\n");

    arrayx = (int **)calloc (*row, sizeof(int*));
    arrayx [*col] = (int *)calloc (*col, sizeof(int));

    for (outer=0; outer < *row; outer++) {
    for (inner=0; inner < *col; inner++)
    arrayx [*row] [*col] = 10;
    /* crashes when row <> col */

    /* (*(*arrayx + outer) + inner) = 1;
    above statement does not work but
    the one before it does seem to
    compile okay */

    } /* end for outer < row */

    return 0;

    } /* end function main */
    [code]

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Suggestions?
    Find the differences?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int i, j;
      int row, col;
      int **array;
    
      printf ( "Enter dimensions (row,col): " );
      fflush ( stdout );
      if ( scanf ( "%d%*c%d", &row, &col ) != 2 ) {
        fprintf ( stderr, "Some kind of input problem\n" );
        return EXIT_FAILURE;
      }
      /*
       * No error checking, it hides the logic
       */
      array = calloc ( row, sizeof *array );
      for ( i = 0; i < row; i++ )
        array[i] = calloc ( col, sizeof *array[i] );
      for ( i = 0; i < row; i++ ) {
        for ( j = 0; j < col; j++ )
          array[i][j] = i * j;
      }
      for ( i = 0; i < row; i++ ) {
        for ( j = 0; j < col; j++ )
          printf ( "%-5d", array[i][j] );
        printf ( "\n" );
      }
      for ( i = 0; i < row; i++ )
        free ( array[i] );
      free ( array );
    
      return EXIT_SUCCESS;
    }
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look to the left to see how code tags work.
    <---

    Beyond that, why are you complicating this by using the pointers 'row' and 'col' when you have already read into integers which hold this information for you?

    [edit]Prelude beat me to the rest of the answer.[/edit]

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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    All was wonderful for a while, the new system for detecting code and prompting posters was working well for a while

    But it seems the noobs have evolved into even more creative ways of making their posts illegible.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But it seems the noobs have evolved into even more creative ways of making their posts illegible.
    Artifical solutions have no effect against natural fools.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with allocating an array with calloc
    By MSF1981 in forum C Programming
    Replies: 2
    Last Post: 05-06-2009, 07:45 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM