char runtime error

Hello, why this code gets runtime error ?

1
2
3
4
5
char b[7];
char a[]= {'m','a','s','t','e','r','a','\0'};
for(int i=0; i<8; i++)
	b[i]=a[i];
cout<<b;
Last edited on
You're going out of the bounds of your array. Your array is 7 elements long, and it goes from 0 to s-1, where s is its size. If you try to go to the 7th element, you'll be accessing information that you're not meant to, and you may get a segmentation fault (which I think you did!). Change the 8 to a 7 and you'll be fine. :)

Happy coding!

-Albatross
thanks :)
Topic archived. No new replies allowed.