Thread: Dynamically allocated 2D array of chars - unkwnown number of rows and columns

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    9

    Dynamically allocated 2D array of chars - unkwnown number of rows and columns

    Dear All,

    I am trying to solve a problem of dynamically allocated 2D array. My goal is to develop program that will scan at the input unknown number of sentences of unknown length in this format: "float number, colon, text" f.e:

    30:How are you?
    80:A dog is an animal.
    35.5:I like ice cream and coke.
    ...

    After the end of inputs, set of questions follows, user wants to find out whether there is matching pattern like:


    Question:
    > how
    Found:
    30:How are you?
    Question:
    > I like
    Found:
    35.5: I like ice cream and coke.
    Question:
    > Cat
    Found:
    nothing

    At first I will have to probably save these sentences dynamically into 2D array with realloc function, but I do not know how to do that if I do not know the length of every sentence nor the number of sentences.

    Thank you for your suggestions.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by arp35
    At first I will have to probably save these sentences dynamically into 2D array with realloc function, but I do not know how to do that if I do not know the length of every sentence nor the number of sentences.
    I suggest defining two structures like this:
    Code:
    struct sentence
    {
        double number;
        char *text;
    };
    
    struct sentence_list
    {
        struct sentence *sentences;
        size_t size;     // number of elements in use
        size_t capacity; // number of elements allocated
    };
    The idea is to read line by line, either using the non-standard (but POSIX standard) getline, or by defining your own version of getline with fgets in a loop to read the entire line of unknown length. You then parse this line (keeping in mind the colon) to get number and text, with which you then create a struct sentence object and insert it into the struct sentence_list object. If the size is about to exceed the capacity, you realloc the sentences for a new capacity (say 1.5 or 2 times the previous capacity).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    If you are curious on how to allocate a bidimensional array, here's a tip:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
      int i, j;
      int ( *ptr )[][20];   // ptr = pointer to bidimensional array of ints.
      size_t size;
    
      // Calc 10 rows needed space on heap.
      size = 10 * sizeof( int [20] );
    
      // Using calloc() to zero the entire array...
      ptr = calloc( 1, size );
    
      //...
      ( *ptr )[5][5] = 10;
      //...
    
      printf( "Allocated: %zu bytes @ %p\n", size, ptr );
    
      // show the array...
      for ( i = 0; i < 10; i++ )
      {
        printf( "row %-2d: ", i );
    
        for ( j = 0; j < 20; j++ )
          printf( "%3d ", ( *ptr )[i][j] );
    
        putchar( '\n' );
      }
    
      free( ptr );
    }

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    9
    Thank you very much. 🙂

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-09-2019, 02:11 PM
  2. Array counting Rows and Columns:
    By jocdrew21 in forum C Programming
    Replies: 7
    Last Post: 07-21-2013, 02:07 PM
  3. Swapping Rows and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 03:40 PM
  4. Addition of the rows and columns in an array.
    By Turtal in forum C Programming
    Replies: 4
    Last Post: 11-14-2006, 09:00 PM
  5. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM

Tags for this Thread