Error: stray '\240' in program

2020-04-03 09:21发布

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 2d 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.

7条回答
干净又极端
2楼-- · 2020-04-03 10:03

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.

查看更多
smile是对你的礼貌
3楼-- · 2020-04-03 10:12

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
查看更多
迷人小祖宗
4楼-- · 2020-04-03 10:14

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.

查看更多
男人必须洒脱
5楼-- · 2020-04-03 10:20

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

查看更多
神经病院院长
6楼-- · 2020-04-03 10:20

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.

查看更多
我想做一个坏孩纸
7楼-- · 2020-04-03 10:23

Your Program has invalid/invisible characters in it. You most likely would have picked up these invisible characters when you copy and past 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.

查看更多
登录 后发表回答