0

I've been working on Fifteen, and I want to insert the values into an array (in init) but for some reason, it doesn't know what 'new' is. When I try #include <new>, it says it cannot find said file. Here's my code for init():

int** init(int d)
{
    int** field = 0;
    field = new int*[d];         //line 163
    int square = d*d - 1;
    int tempSquare = square;

    for(int i = 0; i < d; i++) {
      field[i] = new int[d];
      for(int j = 0; j < d; j++) {
        field[i][j] = tempSquare;
        tempSquare--;
    }
    }
    return field;
}

When I makesaid code, I get the following error message:

fifteen.c:163:13: error: use of undeclared identifier 'new'
field = new int*[d];

(I marked line 163 in init for you) Could someone maybe tell me how I can fix this error. I've been sitting on it for a few hours now and haven't found an answer myself, sadly.

0

It looks like you're trying to use new, a C++ feature, in a C program. new doesn't exist in C.

Also, int is a keyword, so not a great idea to use it as an array name (assuming this isn't another type of C++ construct.). Next, in field = new int*[d]; the asterisk declares a pointer and should follow the type, not the variable name. C also does not allow declaring a variable as the source of an assignment (to the right of the = sign), as was done here.

I'm also curious about the return type of int** Why do you want to return a pointer to a pointer of type int? A void return is sufficient for this function.

There are almost certainly other issues, but there's no reason to go into them until the code has been rewritten to C standards instead of C++.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Well, I have good news and bad news. You answered my question just 5 minutes too late ;) I already got around that problem and got to the next one. Thank you, anyway :) May 14 '16 at 17:03
  • And as for why I used int** as a return type, it's because init returns a 2d array May 14 '16 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .