16 17
|
char myArray[]={};//Initialize arrays
char copyArray[]={};
|
These arrays are still size zero. It doesn't compile for me.
I'm not sure what your compiler is doing, but it surely will not be guessing that you wanted a size of 100. The consequence of that is that there will be a buffer overflow, the program will be overwriting areas of memory which are outside the arrays. This will give undefined behaviour - anything could happen.
Try adding these lines (for information purposes) just after that (at line 18)
18 19
|
cout << "sizeof(myArray) " << sizeof(myArray) << '\n';
cout << "sizeof(copyArray) " << sizeof(copyArray) << '\n';
|
and it will tell you how big the arrays are.
You can use that information at line 20. Instead of this:
|
cin.getline(myArray, 100);
|
replace it with
|
cin.getline(myArray, sizeof(myArray));
|
This way, you can be certain that the getline will not try to store anything beyond the boundaries of the array.
And ... of course, go back to lines 16 and 17 and specify an actual size for the arrays:
1 2
|
char myArray[100] = {}; // Initialize arrays
char copyArray[100] = {};
|
By the way, the function
myStrSwap()
won't work as it is. Trying to swap the pointers here will only affect what happens inside the function. The original arrays will be unchanged.