Home c++ Dynamic Row Array

Dynamic Row Array

Author

Date

Category

Help, please understand what is the error.

# include & lt; iostream & gt;
#Include & lt; CString & GT;
Using Namespace STD;
INT MAIN () {
char * str;
char ** ptr;
For (int i = 0; i & lt; 2; i ++) {
  CIN & GT; & GT; STR;
  INT LEN = STRLEN (STR); // Calculate the length of our line
  ptr [i] = new char [len +1]; // Sell Memory
  STRCPY (PTR [I], STR); // Copy the string of STR in PTR
}
For (int i = 0; i & lt; 2; i ++) {
  COUT & LT; & LT; PTR [i] & lt; & lt; Endl; // Show what is contained in PTR
}
DELETE [] PTR; // Free the allocated memory
Return 0;
}

Answer 1, Authority 100%

Minimum changes so that the code earned:

# include & lt; iostream & gt;
#Include & lt; CString & GT;
Using Namespace STD;
INT MAIN () {
Char str [1024];
char ** ptr;
Const int n = 2;
ptr = new char * [n];
For (int i = 0; i & lt; n; i ++) {
  CIN & GT; & GT; STR;
  INT LEN = STRLEN (STR); // Calculate the length of our line
  ptr [i] = new char [len +1]; // Sell Memory
  STRCPY (PTR [I], STR); // Copy the string of STR in PTR
}
For (int i = 0; i & lt; n; i ++) {
  COUT & LT; & LT; PTR [i] & lt; & lt; Endl; // Show what is contained in PTR
}
For (int i = 0; i & lt; n; i ++) {
  delete [] ptr [i];
}
DELETE [] PTR; // Free the allocated memory
Return 0;
}

code that is written in the C++ rules (although this code is far from ideal). Look, as shorter it became.

# include & lt; iostream & gt;
#Include & lt; CString & GT;
#Include & lt; vector & gt;
Using Namespace STD;
INT MAIN () {
  Vector & lt; String & GT; PTR;
  Const int n = 2;
  For (int i = 0; i & lt; n; i ++) {
    String str;
    CIN & GT; & GT; STR;
    ptr.push_back (STR);
  }
  For (int i = 0; i & lt; ptr.size (); i ++) {
    COUT & LT; & LT; PTR [i] & lt; & lt; Endl; // Show what is contained in PTR
  }
  Return 0;
}
Previous articleSingle WHOIS IP
Next articlePhoneGAP or Cordova?

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions