Thread: how to access dyn. string-arrays?

  1. #1
    Unregistered
    Guest

    how to access dyn. string-arrays?

    Hi,
    i have a problem with dynamically allocated strings...
    I hope it's ok, if i post this in the C forum though i am using the "new"-keyword (beacuse char* = C-style)
    First the code:

    Code:
    char **mult = new char*[20];
    for( int i = 0; i < 20; i++ )
        *( mult + i ) = new char[200];
    This should allocate 20 strings a 200 bytes.
    If i try this, for example-

    Code:
    *( *( mult + 1 ) + 5) = 'a';
    -my program crashes. Strcpy or any other code using this array, crashes, too... How do i access one char or a string from the array not fsckig the program? (please post code)
    thanks.

  2. #2
    Unregistered
    Guest
    Just got it work with malloc() :)

    char **s = ( char** )malloc( 20 );
    *( s ) = ( char* )malloc( 200 );
    strcpy( s[0], "hello" );
    puts( s[0] );

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( ) {
        int i;
        char **mult;
    
        // mult[20][200] coming up...
        mult = malloc( 20 * sizeof(char*) );
        for ( i = 0 ; i < 20 ; i++ ) {
            mult[i] = malloc( 200 * sizeof(char) );
        }
    
        mult[1][5] = 'a';
        return 0;
    }
    > char **s = ( char** )malloc( 20 );
    You forgot the *sizeof(char*)

    Whilst it's big enough for one pointer, it's not enough for 20

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, You could have a little fuction do all the ugly work of allocation:

    char *NewString(int how_big)
    {
    char *out = (char *)malloc(sizeof(char) * how_many);
    return out;
    }


    Then of course:

    char *input = NewString(100);

    ...allocates a 100 char string...
    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;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > char *input = NewString(100);

    Is this really any different than:

    input = malloc( 100 );

    After all, there is no need to typecast the malloc return.

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

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I ran this without crashing.
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <iostream.h>
    int main(void)
    {
    char **mult = new char*[20];
    for( int i = 0; i < 20; i++ )
        *( mult + i ) = new char[200];
    
    for (i=0; i<20; i++)
       strcpy(mult[i],"My favorite food is pizza!!!");
    for (i=0; i<20; i++)
       cout << mult[i] << "..." << endl;
    
    *( *( mult + 1 ) + 5) = 'a';
    *( *( mult + 2 ) + 5) = '*';
    *( *( mult + 3 ) + 5) = '+';
    for (i=0; i<20; i++)
       cout << mult[i] << "..." << endl;
    
    for (i=0; i<20; i++)
       delete []mult[i];
    delete []mult;
    cout << "Done.\n";
    return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I ran this without crashing.

    Your code is C++, not C.

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

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Your code is C++, not C.

    Did you even read the original post?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM