10

It is wanted of me to implement the following function:

void calc ( double* a, double* b, int r, int c, double (*f) (double) )

Parameters a, r, c and f are input and b is output. “a” and “b” are two-dimensional matrices with “r” rows and “c” columns. “f” is a function pointer which can point to any function of the following type:

double function‐name ( double x ) {
    …
}

Function calc converts every element in matrix a, i.e., aij, to bij=f(aij) in matrix b.


I implement the calc function as follows, and put it in a program to test it:

#include <stdlib.h>
#include <iostream>

using namespace std;

double f1(double x){
    return x * 1.7;
}

void calc (double* a, double* b, int r, int c, double (*f) (double))
{
    double input;
    double output;

    for(int i=0; i<r*c; i++)
    {
        input = a[i];
        output = (*f)(input);
        b[i] = output;
    }
}

int main()
{
    // Input array:
    int r=3;
    int c=4;
    double* a = new double[r*c];
    double* b = new double[r*c];

    // Fill "a" with test data
    //...

    for (int i=0; i<r*c; i++)
    {
        a[i] = i;
    }

    // Transform  a to b
    calc(a, b, r, c, f1);

    // Print to test if the results are OK
    //...

    for (int i=0; i<r*c; i++)
    {
        b[i] = i;
    }

    return 0;
}

The problem is, I can't compile it. This is the output of DevC++ when I click on Compile and Execute button:

Compilation errors referring to invalid characters

What's wrong?

I appreciate any comment to make the implementation more efficient.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ebrahim Ghasemi
  • 5,850
  • 10
  • 52
  • 113
  • 13
    `\240` is non-breaking space in iso8859-1 encoding, so the mystery is how those very special characters got in there. Did you write the code in Word or something? – Wintermute Mar 27 '15 at 23:19
  • 1
    @Wintermute hehe I actually double-checked that the OP wasn't the same [person I met 16m ago](http://stackoverflow.com/questions/29311287/bad-access-either-in-stdmap-or-stdstring-destructor#comment46816709_29311287) :) – sehe Mar 27 '15 at 23:21
  • 2
    The code does compile fine for me (g++ -o test.exe test.cpp) without any alterations. Did you check around line 10? – jensa Mar 27 '15 at 23:25
  • 1
    @Wintermute Yes you are right. I rewrite the 10th line (The line that errors refers to) and problem solved. Thanks. – Ebrahim Ghasemi Mar 27 '15 at 23:26
  • BTW, Is the implementation correct?(As it wanted to be) – Ebrahim Ghasemi Mar 27 '15 at 23:27
  • 1
    You never clean up the memory you allocated (protip: look into [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) to replace those naked pointers). Other than that it looks fine. @sehe Huh. I saw another question with the exact same Unicode quotes just a few days ago. It left me wondering. I mean, it's really hard to get those characters by accident in any decent IDE. – Wintermute Mar 27 '15 at 23:30
  • 1
    Also copying and pasting from Adobe Acrobat and Adobe Reader causes that error. Sublime Text might be handy to eliminate these unwanted characters. – ozanmuyes Nov 15 '15 at 21:22
  • 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 May 06 '23 at 22:33
  • *"It is wanted of me "* and the use of 'smart quotes' in *"“a” and “b” are two-dimensional"* suggests the code has been copied from somewhere. Some teachers distribute homework assignments as PDF files (!!), probably unaware of the problems this is causing. – Peter Mortensen May 06 '23 at 22:44

9 Answers9

11

As mentioned in a previous reply, this generally comes when compiling copy pasted code. If you have a Bash shell, the following command generally works:

iconv -f utf-8 -t ascii//translit input.c > output.c
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
the sudhakar
  • 49
  • 3
  • 9
6

It appears you have illegal characters in your source. I cannot figure out what character \240 should be but apparently it is around the start of line 10

In the code you posted, the issue does not exist: Live On Coliru

sehe
  • 374,641
  • 47
  • 450
  • 633
  • You are right. I rewrite the 10th line (The line that errors refers to) and problem solved. Thanks. BTW Is the implementation correct?(As it wanted to be) – Ebrahim Ghasemi Mar 27 '15 at 23:28
  • Tempted to say "I don't know. How correct did it want to be?". Having a look – sehe Mar 27 '15 at 23:32
  • 2
    Looks okay, but quite clumsyu and error prone, I'd **at least** prefer [this (live demo)](http://coliru.stacked-crooked.com/a/58500d4fb13d356d) or even better [c++ style](http://coliru.stacked-crooked.com/a/e6df7890349cbe11) or even [why use a target vector](http://coliru.stacked-crooked.com/a/2e3cb4aa7c4261f1) – sehe Mar 27 '15 at 23:45
  • 1
    The illegal characters are probably Unicode NO-BREAK SPACE characters, encoded as "\302\240" (octal). Non-breaking spaces are used all the time on web pages, especially when stuff like code shouldn't get line-wrapped by the browser. – interfect Mar 06 '17 at 21:01
  • It could be *just* 240 octal, 0xA0 - [CE/CP-1250](https://en.wikipedia.org/wiki/Windows-1250#Character_set)'s 'NBSP'. – Peter Mortensen Aug 03 '21 at 23:53
5

The /240 error is due to illegal spaces before every code of line.

eg.

Do

printf("Anything");

instead of

 printf("Anything");

This error is common when you copied and pasted the code in the IDE.

Vishesh Gautam
  • 186
  • 1
  • 8
2

Your program has invalid/invisible characters in it.

You most likely would have picked up these invisible characters when you copy and paste code from another website or sometimes a document. Copying the code from the site into another text document and then copying and pasting into your code editor may work, but depending on how long your code is you should just go with typing it out word for word.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

I got the same error when I just copied the complete line but when I rewrite the code again i.e. instead of copy-paste, writing it completely then the error was no longer present.

Conclusion: There might be some unacceptable words to the language got copied giving rise to this error.

Ajay
  • 130
  • 6
1

Solution: Delete that line of code (if you copied it from another source document) and type it yourself.

Error: stray '\240' in program is simply a character encoding error message.

From my experience, it is just a matter of character encoding. For example, if you copy a piece of code from a web page or you first write it in a text editor before copying and pasting in an IDE, it can come with the character encoding of the source document or editor.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rocksyne
  • 1,264
  • 15
  • 17
0

I faced the same problem due to illegal spaces in my entire code.

I fixed it by selecting one of these spaces and use find and replace to replace all matches with regular spaces.

Keith OYS
  • 2,285
  • 5
  • 32
  • 38
0

I did the following to solve the issue:

  • Paste the corrupted code into a txt file.
  • Open the code file with some hexadecimal editor.
  • Replace every occurrence of "C2A0" hexadecimal chain that you find in the document with an empty string.
  • Save it.

Now you should be able to copy the text code without any issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • It is much easier to use regular expression `\x{00A0}` to search/replace in any modern text editor or IDE (note: The notation is different in Visual Studio Code (and probably others): `\u00A0` (instead of `\x{00A0}`)). 0xC2 0xA0 (hexadecimal) → UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=156&number=128)). – Peter Mortensen May 06 '23 at 22:52
-1

I cleared all the red spaces in the code. I.e., error spaces. Then the code worked just fine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131