Thread: Multidimentional array

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Multidimentional array

    Im trying to strcat() text[] into buffer[] but i get an error.

    What am i doing wrong?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int  c = 5;
        char text[3] = {'Hello','brave','world!'};
        char **dynamicArray = 0,  buffer[256] = {0};
    
        dynamicArray = new char *[c];
    
        for( int i = 0 ; i < c ; i++ )
             dynamicArray[i] = new char[3];
    
        for( int i = 0 ; i < 3 ; i++ )
        {
             strcat(buffer, text[i]);
        }
        cout << buffer << endl;
    
        //free the allocated memory
        for( int i = 0 ; i < c ; i++ )
        delete [] dynamicArray[i] ;
        delete [] dynamicArray ;
    
        return 0;
    }
    Last edited by Ducky; 11-01-2009 at 09:45 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that character literals are single quoted, but string literals are double quoted, and a string literal is convertible to a pointer to its first element. As such, this:
    Code:
    char text[3]  = {'Hello','brave','world!'};
    should be:
    Code:
    const char* text[3]  = {"Hello", "brave", "world!"};
    By the way, you should just remove the whole deal concerning dynamicArray since it is not relevant to what you are asking about.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also recall that 3 characters are not enough to hold the strings. Furthermore, you use strcat on an uninitialized string.
    I would just use a vector of vector instead of new, however. And std::string for that matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you very much Laselight and Eysia!

    >>and a string literal is convertible to a pointer to its first element.

    I didnt know that, its very useful, thanks.

    >>you should just remove the whole deal concerning dynamicArray

    Yes its not relevant but thats what im trying to learn and its in the process.
    I think we cant change the title of the thread so if you have the power you can rename it if you want.

    >>Also recall that 3 characters are not enough to hold the strings.

    I thought they were 3 elements.

    >>Furthermore, you use strcat on an uninitialized string.

    Which one is uninitialized? buffer[256] is initialized to 0.

    >>I would just use a vector of vector instead of new, however. And std::string for that matter.

    Would it still be dynamic with vectors because i would need dynamic allocation.
    Last edited by Ducky; 11-01-2009 at 10:32 AM.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    >>Also recall that 3 characters are not enough to hold the strings.

    I thought they were 3 elements.
    3 elements of 1 char, yes.
    We have the inner layer and the outer layer.
    The outer layer specifies the number of strings you want and the inner layer specifies the maximum length of the string you want.

    >>Furthermore, you use strcat on an uninitialized string.
    Which one is uninitialized? buffer[256] is initialized to 0.
    Ah, but you are doing it on buffer. Of course, of course. I did not see that. No problem then.

    >>I would just use a vector of vector instead of new, however. And std::string for that matter.

    Would it still be dynamic with vectors because i would need dynamic allocation.
    vector would handle the dynamicness of the array. If you want to try making dynamic arrays yourself, I would not use it, I suppose.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Cool, thank you!

    I go with vectors if you say that its preferable.
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, of course, it is more preferable than native arrays since it is less error prone, meaning safer programs and usually less development time due to less testing and debugging. But you will not learn anything about manual managing of dynamic arrays if that is what you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. A multidimentional array to a class.
    By scarface in forum C++ Programming
    Replies: 3
    Last Post: 07-12-2006, 04:42 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM