Thread: Malloc strings?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Sebastiani View Post
    Are you compiling this with a C or C++ compiler? If the latter, it's because C++ doesn't allow implicit casts...
    I'm unsure to be honest. I'm working on the terminal that comes with ubuntu and I'm using gcc with no flags to compile the file.

    Quote Originally Posted by Sebastiani View Post
    You don't need it because it's complete nonsense. Consider this: sizeof(strlen(s)+1) == sizeof(0).

    ****
    EDIT: Post your current code. The error message could be related to the typedef...
    My current code isn't the issue (at least it shouldn't be). I'm just trying to figure out how I would insert the string s into an array of strings messages[] so that I can continue with my assignment.

    In my assignment, I have a test file that has the messages array and a string. It passes messages and the string into a function. In that function, I determine what the round is in the game that's being played, and so I need to insert the string into messages[round] so that I can refer back to it in the future.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Mentallic View Post
    My current code isn't the issue (at least it shouldn't be).
    If your code wasn't the issue, it'd compile.
    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;
    }

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by MutantJohn View Post
    Why not just use strcpy?
    Does the array have room? The messages[] that doesn't actually have a size associated with it at compile time is confusing me. How much room is there? Can I put as many strings in as I want?

    Quote Originally Posted by Sebastiani View Post
    If your code wasn't the issue, it'd compile.
    Sorry, I thought you were referring to my assignment files, not this code. What I currently have is what std10093 posted in post #2.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Mentallic View Post
    What I currently have is what std10093 posted in post #2.
    Dandy, then it should compile just fine.

    Quote Originally Posted by Mentallic View Post
    The messages[] that doesn't actually have a size associated with it at compile time is confusing me.
    Sure it does. You've declared it as MESSAGE_SIZE, right?
    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
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by MutantJohn View Post
    First, give messages a size. I am assuming what you are using is a 2d char array where each element of messages is playerMessage and as such already has a size.

    If that is the case, just strcpy into an element of messages.
    But I don't know how many strings I'll need to put into messages. If I make it messages[n] then I'm set on having only up to n strings, but may need more.


    Quote Originally Posted by Sebastiani View Post
    Dandy, then it should compile just fine.
    But my assignment has a .h file that the c file has included, and it has playerMessage defined as
    typedef char playerMessage[MESSAGE_SIZE];

    so I need to incorporate that into the code somehow, don't I?



    Quote Originally Posted by Sebastiani View Post
    Sure it does. You've declared it as MESSAGE_SIZE, right?
    playerMessage[MESSAGE_SIZE] is each string, but I have to store all of the strings in messages[].

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Mentallic View Post
    But I don't know how many strings I'll need to put into messages. If I make it messages[n] then I'm set on having only up to n strings, but may need more.




    But my assignment has a .h file that the c file has included, and it has playerMessage defined as
    typedef char playerMessage[MESSAGE_SIZE];

    so I need to incorporate that into the code somehow, don't I?





    playerMessage[MESSAGE_SIZE] is each string, but I have to store all of the strings in messages[].
    Sorry, I misread your post, apparently. Does this help?

    Code:
    int main( void ) 
    {
        const int 
            STRING_COUNT = 10,
            STRING_LENGTH = 100;
        int
            index;
        char**
            strings = malloc( STRING_COUNT * sizeof( char* ) );
        for( index = 0; index < STRING_COUNT; ++index )
            strings[ index ] = malloc( STRING_LENGTH * sizeof(char) );
        /* do something useful here */
        for( index = 0; index < STRING_COUNT; ++index )
            free( strings[ index ] );
        free( strings );    
        return 0;
    }
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: malloc and array of strings
    By 0ooo0 in forum C Programming
    Replies: 6
    Last Post: 03-18-2013, 08:19 AM
  2. Quick question on malloc and strings
    By officedog in forum C Programming
    Replies: 20
    Last Post: 11-06-2008, 05:14 PM
  3. how to use malloc for array of strings
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 04:19 PM
  4. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  5. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 10:46 PM