how to access dyn. string-arrays?

This is a discussion on how to access dyn. string-arrays? within the C Programming forums, part of the General Programming Boards category; Hi, i have a problem with dynamically allocated strings... I hope it's ok, if i post this in the C ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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,699
    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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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, 04:45 PM
  2. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21