C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-01-2009, 09:42 AM   #1
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 431
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;
}
__________________
Using Code::Blocks,MingW with Windows.

Last edited by Ducky; 11-01-2009 at 09:45 AM.
Ducky is online now   Reply With Quote
Old 11-01-2009, 09:47 AM   #2
C++ Witch
 
laserlight's Avatar
 
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!'};
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
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   Reply With Quote
Old 11-01-2009, 09:56 AM   #3
Mysterious C++ User
 
Elysia's Avatar
 
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-01-2009, 10:29 AM   #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   Reply With Quote
Old 11-01-2009, 10:54 AM   #5
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
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.

Quote:
>>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.

Quote:
>>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.
__________________
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-01-2009, 11:10 AM   #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   Reply With Quote
Old 11-01-2009, 01:29 PM   #7
Mysterious C++ User
 
Elysia's Avatar
 
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:24 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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