Why copy remains empty after every assgment in for() ?

int main()
{
	const char* c = "abcde";
	int size = strlen(c);
	char* copy = new char[size];
	for(const char* p = (c + size - 1); p >= c; p--)
	{
		*copy++ = *p;
	}
	copy[size] = 0;
	for(int i = 0; i < size; i++)
		cout << copy[i];
	delete [] copy;

}

Recommended Answers

Your incrementing copy then deleting it....How does this not crash?

Jump to Post

All 3 Replies

Your incrementing copy then deleting it....How does this not crash?

#include <stdio>
#include <stdlib>

int main ()  {
   char a[]={"abcdefghijklmn"};
   char b[sizeof(a)];
   
   int sz=0;
   char* p=&a[sizeof(a)-1];
   
   while (p!=&a && --p)  {
      b[sz++]=*p;
    }
   b[sz++]='\0';
   printf("%s",b);
 }
copy[size] = 0;	
for(int i = 0; i < size; i++)		
    cout << copy[i];

Lets say copy has 10 elements, now he is assigning all those elements a value of 0 and showing it.

Be a part of the DaniWeb community

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