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!

Recommended Answers

All 8 Replies

>void main()
int main()

>if((StringPtr = new char[26]) == NULL)
This may or may not correctly handle errors depending on how old your compiler is.

>*(StringPtr+i) = String;
You know, you can do StringPtr[i] = String[i]; too. It's a lot easier to read and write.

>cout << "StringPtr = " << StringPtr << endl;
You forgot the last character, the '\0'.

>delete [] String;
And why are you trying to delete an array?

>For "delete [] String;" part, I tried to use the for loop to see if it works.
Why don't you take that entire line away and see if it works? You're not supposed to release memory unless you explicitly allocate it.

>*(StringPtr+i) = String;
You know, you can do StringPtr[i] = String[i]; too. It's a lot easier to read and write.

>cout << "StringPtr = " << StringPtr << endl;
You forgot the last character, the '\0'.

That's what I thought about "delete [] String;". I commented this out.

Now, for StringPtr aspect, I agree with you about StringPtr is easier to read and write than *(StringPtr+1). I tried StringPtr and it still contains same data so I thought there must be something to fix from *(StringPtr+1) which is not true. Thank you. Now back to '\0'. What do you mean I forgot the last character, the '\0'?

I tried to add:

if(String[i+1] == '\0')
            String[i+1] = String[i+1];

It still have same output. Am I missing something here?

> strcpy(String, "abcdefghijklmnopqrstuvxyz");
> if((StringPtr = new char[26]) == NULL)
The alphabet, plus a \0 is 27 chars in total.
Both the array and your allocated memory are overstepping their limits.

And when you strcpy(String, "abcdefghijklmnopqrstuvxyz") ; into a 26 character array like this you just blew past your array bounds because a 27th character (\0) is loaded too. Remember, a string in C always ends in \0 and that must be accounted for in the buffer.

> strcpy(String, "abcdefghijklmnopqrstuvxyz");
> if((StringPtr = new char[26]) == NULL)
The alphabet, plus a \0 is 27 chars in total.
Both the array and your allocated memory are overstepping their limits.

Correct. Alphabet contains 26 chars.
That's why I use for(i = 0; String[i] != '\0'; i++) so that way, it won't include the 27th char.

For example:

for(i = 0; String[i] != '\0'; i++)
    {
        cout << String[i];
        StringPtr[i] = String[i];
        cout << StringPtr[i] << " ";
    }
    cout << endl << "String = " << String << endl;
    cout << "StringPtr = " << StringPtr << endl;

Output:
==========================================
aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz
String = abcdefghijklmnopqrstuvwxyz
StringPtr = abcdefghijklmnopqrstuvwxyz²²²²½½½½½½½½ε■
==========================================

You can see the matched data between String and StringPtr from the for loop statement. Then why didn't StringPtr itself contains exactly datas it showed in the loop statement for the cout part.

> so that way, it won't include the 27th char.
That being the \0 character,
That being the character which stops cout from printing trailing garbage.

Your choice.

I figured it out.

Just remove for loop statement and use assignment operator:

StringPtr = String;

and remove:

delete[] StringPtr;

Now both are identical. Both's output are matched. :)

Thank you for giving some of your time to work out with me. :)

You don't have enough memory for all the letters + the null character. Allocate one more character to each buffer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.