![]() |
| | #1 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 431
| Multidimentional array 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;
}
__________________ Using Code::Blocks,MingW with Windows. Last edited by Ducky; 11-01-2009 at 09:45 AM. |
| Ducky is online now | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,344
| 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!'};
Code: const char* text[3] = {"Hello", "brave", "world!"};
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #4 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 431
| 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.
__________________ Using Code::Blocks,MingW with Windows. Last edited by Ducky; 11-01-2009 at 10:32 AM. |
| Ducky is online now | |
| | #5 | ||||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Quote:
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. Quote:
Quote:
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||||
| Elysia is offline | |
| | #6 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 431
| Cool, thank you! I go with vectors if you say that its preferable.
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is online now | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| 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.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| from 2D array to 1D array | cfdprogrammer | C Programming | 17 | 03-24-2009 10:33 AM |
| Help with Searching Array for Longest Sequence of an Element | w2look | C Programming | 7 | 11-25-2008 01:50 AM |
| A multidimentional array to a class. | scarface | C++ Programming | 3 | 07-12-2006 04:42 AM |
| Unknown Memory Leak in Init() Function | CodeHacker | Windows Programming | 3 | 07-09-2004 09:54 AM |
| Quick question about SIGSEGV | Cikotic | C Programming | 30 | 07-01-2004 07:48 PM |