We help IT Professionals succeed at work.

2D array

hpchong7
hpchong7 asked
on
Dear all,

    I want to creat a 2D array as follows:

char Message[40][2];

in which Message[0][0] will store an english sentences, while Message[0][1] will store an Chinese sentences.
Totally there are 40 english sentences, 40 chinese sentences.However, I don't know what's wrong with my code:

char **Message[40][2];
strcpy(*Message[0][0],"An English Sentence.");
strcpy(*Message[0][1],"A Chinese Sentence.");
printf("%s",Message[0][0]);
printf("%s",Message[0][0]);

There is nothing print out.May you help me to debug?Thanks!
Comment
Watch Question

Commented:
First off, this will print something:
char Message[2][40];
strcpy(Message[0],"An English Sentence.");
strcpy(Message[1],"A Chinese Sentence.");
printf("%s",Message[0]);
printf("%s",Message[1]);

Commented:
BUT:
It won't do what you want. The '40' refers to the number of chars in the sentence, not the number of sentences.  You need to think about what you're doing here:

char **Message[40][2];
sets up a _pointer_ to an array of char, not the array itself.  So you're not allocating any memory.

This is the source of all of your errors.  So in the code I posted, I'm setting up _two_ strings, each of 40 characters.

There is a better way of doing this - use std::string to store your strings.  If you're interested, ask.  Otherwise, in order to do what you want, you'll need to set up a 3D array:

char Message[100][2][40];
strcpy(Message[0][0],"An English Sentence.");
strcpy(Message[1][0],"A Chinese Sentence.");
printf("%s",Message[0][0]);
printf("%s",Message[1][0]);

This has 100 characters maximum length for each sentence: you'll stomp all over memory if you try using more.

Commented:
Oops:  What I wrote there had 100 sentences, each of 40 characters.
CERTIFIED EXPERT
Author of the Year 2009

Commented:
char **Message[40][2];

This just sets up a 2D array for pointers to pointers.  You have not allocated any space to store the text.

There are several ways to do this.  I'd use:

const int CNUM_English= 0;
const int CNUM_Chinese= 1;

char* apszMessages[40][2];
apszMessage[0][CNUM_English]= new char[100]; // or whatever
strcpy( apszMessage[0][CNUM_English], "THis is English" );

apszMessage[0][CNUM_Chinese]= new char[100]; // or whatever
strcpy( apszMessage[0][CNUM_Chinese], "This is Chinese" );

Then you would need to remember to use...
   delete apszMessage[0][CNUM_English];
   delete apszMessage[0][CNUM_English];
...later on.

Now lets suppose that you knew all messages were less than 100 bytes.  You could use:

  char apszMessages[40][2][100]; 40x2 100-bytes strings

  strcpy( apszMessage[0][CNUM_English], "THis is English" );
  strcpy( apszMessage[0][CNUM_Chinese], "This is Chinese" );

Such a table is wasteful but not really all that much and it is convenient to use and easy to understand.

HOWEVER, it looks like you may be initializing a static array -- messages that don't need to be changed during the life of the program.  In that case, just let the compiler store pointers to literal strings:

const char* apszMessages[40][2] = {
    { /*string 0*/ "THis is English", "This is Chinese" },
    { /*string 1*/ "Also is English", "Also is Chinese" },
     ...
    { /*string 39*/ "Last in English", "Last in Chinese" }
};

That way you don't need to use strcpy to put the data in place and you don't need to allocate any space.

-- Dan


Author

Commented:
Dear both,
   thank you very much for your detail and concise explanation!It's helpful to me!Dan Rollins,your last method is very good indeed!I want to use it, but I encounter error again:

class DisplayError{
public:
const char* apszMessages[40][2] = {
    { /*string 0*/ "THis is English", "This is Chinese" },
    { /*string 1*/ "Also is English", "Also is Chinese" },
};
    DisplayError();
    ~DisplayError();
     
};

The complier said it has syntax error on line 3 (which declares the apszMessages)May you teach me how to solve it?Thanks!
CERTIFIED EXPERT
Author of the Year 2009
Commented:
It is a C++ syntax error.  I know that the error message is not too helpful!

You are trying to initialize the array in the class definition.  That is forbidden,  just like trying to use:
 
class MyClass {
   int m_SomeMember=3;  // no can do!
}

Do, make the array of pointers a CONST value and intitialize them in the CPP file:


#include <windows.h>

class DisplayError {
public:
    static LPCSTR apszMessages[40][2];
    DisplayError() {};
    ~DisplayError() {};
    LPCSTR GetErrMsgEnglish( int nErrNum ) { return apszMessages[nErrNum][0]; };
    LPCSTR GetErrMsgChinese( int nErrNum ) { return apszMessages[nErrNum][1]; };

};

//-------------------------------- put this in the CPP file (not the .H file)
LPCSTR DisplayError::apszMessages[40][2] = {
   { /*string 0*/ "THis is English", "This is Chinese" },
   { /*string 1*/ "Also is English", "Also is Chinese" },
};


void main()
{
    DisplayError de;
    LPCSTR p= de.apszMessages[0][1]; // get chinese msg #0

    char szTmp[256];
    wsprintf( szTmp, "English msg #1 is\n '%s'", de.GetErrMsgEnglish(1) );
    MessageBox( 0, szTmp,"Test", MB_OK );
}

-- Dan
CERTIFIED EXPERT
Author of the Year 2009

Commented:
oops, I meant to say...

So, declare the array of pointers as STATIC and intitialize them in the CPP file...

-- Dan
You were just barely off... look at how I changed the first line to be a 2D array of pointers.  Then, the second section is needed to actually allocate memory for each pointer so that they can hold the sentence you want them to hold.

//40 sets of 2 character pointers to point at strings
char *Message[40][2];

//allocate memory to hold sentences
Message[0][0] = new char[ strlen("An English Sentence.")+1 ];
Message[0][1] = new char[ strlen("A Chinese Sentence.")+1 ];

strcpy(Message[0][0], "An English Sentence.");
strcpy(Message[0][1],"A Chinese Sentence.");
printf("%s\n",Message[0][0]);
printf("%s\n",Message[0][1]);


Author

Commented:
Dear all,
  Thank you very very much!All of Your skill is really excellent!But as I can only grade 1 person, Dan Rollins's code suits me the best!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.