[c++] Error: stray '\240' in program

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.

This question is related to c++ arrays function pointers matrix

The answer is


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.


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

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.


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.


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.


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.


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers

Examples related to pointers

Method Call Chaining; returning a pointer vs a reference? lvalue required as left operand of assignment error when using C++ Error: stray '\240' in program Reference to non-static member function must be called How to convert const char* to char* in C? Why should I use a pointer rather than the object itself? Function stoi not declared C pointers and arrays: [Warning] assignment makes pointer from integer without a cast Constant pointer vs Pointer to constant How to get the real and total length of char * (char array)?

Examples related to matrix

How to get element-wise matrix multiplication (Hadamard product) in numpy? How can I plot a confusion matrix? Error: stray '\240' in program What does the error "arguments imply differing number of rows: x, y" mean? How to input matrix (2D list) in Python? Difference between numpy.array shape (R, 1) and (R,) Counting the number of non-NaN elements in a numpy ndarray in Python Inverse of a matrix using numpy How to create an empty matrix in R? numpy matrix vector multiplication