My function:

int podchcnt(int arr[]) { int res; for (int j=1;j<=n;j++) { if (arr[j]==1) { res+=1; res+=podchcnt(arr[j]); } } return res; } 

The compiler gives an error:

invalid conversion from 'int' to 'int*' [-fpermissive]

Why? I assign an integer value to an integer variable. The arguments of the function have a one-dimensional array, in a recursive call I transfer a one-dimensional array.


Full code: http://pastebin.com/xW8sQiXC . The digraph is specified by the adjacency table. The program should produce the number of descendants of each vertex starting from the first.

  • Swears at the string "res + = podchcnt (arr [j]);" - TrueRacoon

3 answers 3

Of course, I am not an expert in C ++, but in my opinion you are here res+=podchcnt(arr[j]); transfer not an array, but an array element (i.e., type int instead of int []). Hence the error that says that you cannot convert an int to an int [].

  • Yes you are right. Instead of res+=podchcnt(arr[j]); in the procedure, res+=podchcnt(a[j]); had to be called res+=podchcnt(a[j]); because a is a two-dimensional array, a [j] is one-dimensional in this case, arr [j] is just an element of the array. - TrueRacoon

See for yourself - you have announced

 int podchcnt(int arr[]) 

those. that the podchcnt function takes an array , in other words, a pointer to its first element.
And what do you tell her in podchcnt(arr[j]) ?
Just an element ...

Here is such a discrepancy and leads to problems.

Did not go into the task, but I can assume that you want to transfer the subarray, starting with the j -th element? Then write either arr+j or &arr[j] .

And again, let me remind you that the counting of elements in an array starts from zero , so that the for (int j=1;j<=n;j++) loop for (int j=1;j<=n;j++) causes certain doubts, as well as other cycles in your full text.

Also, you have used the uninitialized variable res - it has no initial value , it can have anything, so the result can be any :) Initialize it, declaring int res = 0; .

Your recursion is unlikely to be finite - only if all arr[j] not units - since in each of its calls the same cycle will be executed, endlessly making the same calls. In this case, since you will transfer all the smaller parts of the array, there will be going beyond the bounds of the array with all the (unpleasant) consequences ...

Well, also - in this case it is not a mistake, it just doesn’t turn out quite ... I won’t even find the words. It’s not quite nice whether to pass an array as a parameter, and its size as a global variable. Pass both values ​​as parameters - then the function will be much more universal. This is also important for recursion.

    Apparently it should be like this:

     res+=podchcnt(arr + j); 

    Since the function accepts an array (count the pointer), then it is necessary to pass a pointer, not an integer.

    However, in this case we must not forget that because of the recursion, the cycle for the residual part should already be less. It is more correct to pass the length (global n ) together with the pointer to the function. But that's another story.