Hello,
This is my first time using forum to ask around for some assistance.
I will make it briefly.
void main()
{
char *StringPtr; //pointer of char
char String[26]; //array of chars
strcpy(String, "abcdefghijklmnopqrstuvxyz");
if((StringPtr = new char[26]) == NULL)
cerr << "\n\a allocation failed" << endl;
else
cout << "allocation was successful" << endl;
for(i=0;String[i] != '\0'; ++i)
*(StringPtr+i) = String[i];
cout << "String = " << String << endl;
cout << "StringPtr = " << StringPtr << endl;
delete [] String; //C4154 Warning...
delete [] StringPtr;
}
Both String and StringPtr contains different data.
How to make StringPtr identical to String?
Output:
========================================
allocation was successful
String = abcdefghijklmnopqrstuvwxyz
StringPtr = abcdefghijklmnopqrstuvwxyz²²²²½½½½½½½½ε■
========================================
For "delete [] String;" part, I tried to use the for loop to see if it works.
for(i=0;i<26;++i)
delete String;
not working.. I understand this for loop statement is for array of pointer but I have no idea why "delete [] String" is not working even it is only array of char not pointer unless the destructor already have its job for array of char that I do not need to do the "delete [] String;"
This is one of my homework, I have discovered 23 out of 25 errors. Two errors remain which is "delete String" and "two strings identical".
Thank you!