Multidimentional array

This is a discussion on Multidimentional array within the C++ Programming forums, part of the General Programming Boards category; Im trying to strcat() text[] into buffer[] but i get an error. What am i doing wrong? Code: #include <iostream> ...

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

    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 08:45 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    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
    796
    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 09:32 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    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
    796
    Cool, thank you!

    I go with vectors if you say that its preferable.
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    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, 12: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

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