>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[i];
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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
> 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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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, astring in C always ends in \0 and that must be accounted for in the buffer.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
> 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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953