0

​​​​​I've been trying to make the strcpy function for practice purposes and came across this problem; I wasn't really able to find the problem in this piece of code, but even in the original function it gives an error when trying to copy a string into another string smaller in size.

This is the error:

(43:2: error: stray ‘\342’ in program
{​​​​​
^)***

The code:

#include <iostream>
#include <string>

using namespace std;

void my_strcpy (char s1[], char s2[])
{​​​​​
  int count = 0;
  while (s2[count] != '\0')
  {​​​​​
    s1[count]= s2[count];
    count++;
  }​​​​​
  s1[count] = '\0';
  cout << "the first array after copying is : " << s1 << endl;
  cout << "the second array after copying is : " << s2 << endl;
}​​​​​

int main ()
{​​​​​
  char x[] = "Mah";
  char y[] = "Mahmoud";

  my_strcpy(y, x) << endl;

  cout << "x = " << x << endl;

  cout << "y = " << y << endl;

  return 0;
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
  • You have stray characters in your code. Voting to close as a typo. – Stephen Newell Jan 21 '21 at 17:05
  • 3
    I would guess you've copied code from somewhere that is using smart quotes, like a word processor or web page. – Retired Ninja Jan 21 '21 at 17:20
  • So look closely around that line, and examine which characters are in your source code. Use a hex editor to be sure. – Asteroids With Wings Jan 21 '21 at 18:18
  • In the code as posted, there are a lot of 0xE2 0x80 0x8B (hexadecimal) → UTF-8 sequence for Unicode code point U+200B ([ZERO WIDTH SPACE](https://www.charset.org/utf-8/9)). Thus triplets of "error stray" \342 \200 \213 (octal) are expected. The errors are undereported in the question. – Peter Mortensen Apr 30 '23 at 22:29
  • ZERO WIDTH SPACE can be searched for (and replaced/deleted) using regular expression `\x{200B}` in any modern text editor or IDE (the notation is different in [Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code) (and probably others): `\u200B` (instead of `\x{200B}`)). There are 25 ZERO WIDTH SPACE in the code as posted (and none of the common observed stray characters). – Peter Mortensen Apr 30 '23 at 22:35
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 30 '23 at 22:39

0 Answers0