[英]Error when compiling : incompatible types and similar warnings ( picture attached )
I'm writing a function that returns the inverse of an array using recursivity but I keep getting these warnings:我正在编写一个 function ,它使用递归返回数组的倒数,但我不断收到这些警告:
Here is my code这是我的代码
#include <stdio.h>
#include <stdlib.h>
int inv( float* t[],int n)
{ float u;
if (n==0) return 0;
else
{
u=*t;
*t=*(t+n);
*(t+n)=u;
return(inv(*(t+1),n-1));
}
}
int main()
{
float t[]={1,2,3,4,5,6,7,8,9};
int n=sizeof t /sizeof *t;
int i=0;
inv(t,n);
for(i=0;i<n;i++)printf("%f",t[i]);
return 0;
}
int inv( float* t[],int n)
int inv(float* t[],int n)
Here, float* t[]
declares an array of pointers.在这里,
float* t[]
声明了一个指针数组。 Please note that *t[i] = *(*(t+i))
请注意
*t[i] = *(*(t+i))
For i = 0, *(*(t+i)) = *(*(t)) = *(*t)
.对于 i = 0,
*(*(t+i)) = *(*(t)) = *(*t)
。
Here, *t
is of type float*
, and u
is of type float.这里,
*t
是float*
类型,而u
是 float 类型。
Using the expression u=*t;
使用表达式
u=*t;
gives you that error (assigning float*
value to float
)给你那个错误(将
float*
值分配给float
)
Solution: Change int inv( float* t[],int n)
to int inv( float t[],int n)
解决方法:将
int inv( float* t[],int n)
改为int inv( float t[],int n)
I use a procedure to arrange the array elements in descending order我使用一个过程以降序排列数组元素
#include <stdio.h>
#include <stdlib.h>
void revs(int i, int n, float *arr)
{
if(i==n)
{
return ;
}
else
{
revs(i+1, n, arr);
printf("%.f ", arr[i]);
}
return 0;
}
int main()
{
float t[]={1,2,3,4,5,6,7,8,9};
int n=sizeof t /sizeof t[0];
int i=0;
revs(0,n,t);
}
It looks like the intended action of OP's function inv
is to reverse the elements of an array of n
float
values in place, using recursion.看起来 OP 的 function
inv
的预期操作是使用递归来反转n
float
值数组的元素。
In the original inv
code, the parameter declaration float* t[]
will be adjusted to be equivalent to float** t
by the compiler.在原
inv
代码中,参数声明float* t[]
会被编译器调整为等价于float** t
。 This needs to be changed to float t[]
or equivalently float* t
.这需要更改为
float t[]
或等效的float* t
。
In the original inv
code, the return value is either 0
or the result of the recursive call to itself, which can only be 0
.在原始的
inv
代码中,返回值要么是0
,要么是递归调用自身的结果,只能是0
。 Therefore the return value is of no use.因此返回值是没有用的。 It would be better to change the return type of the function to
void
.最好将 function 的返回类型更改为
void
。
In the inv
function, the parameter n
is set to the length of the array in the initial call from main
.在
inv
function 中,参数n
设置为来自main
的初始调用中的数组长度。 The function swaps the values of elements at indices 0 and n
before recursing, but there is no element at index n
. function 在递归之前交换索引 0 和
n
处元素的值,但索引n
处没有元素。 The last element has index n-1
.最后一个元素的索引为
n-1
。 The function should swap the elements at indices 0 and n-1
before recursing. function 应该在递归之前交换索引 0 和
n-1
处的元素。 After swapping the first and last elements, the remaining n-2
elements with indices from 1 to n-2
can be swapped by a recursive call.交换第一个和最后一个元素后,剩余的
n-2
索引从 1 到n-2
2 的元素可以通过递归调用进行交换。 The original inv
code uses the value n-1
in the recursive call, but it should be n-2
.原始
inv
代码在递归调用中使用值n-1
,但它应该是n-2
。
If n
is less than 2, the inv
function does not need to do anything.如果
n
小于 2,则inv
function 不需要做任何事情。
A possible implementation of inv
based on OP's original code follows:基于 OP 原始代码的
inv
可能实现如下:
void inv(float* t,int n)
{
float u;
if (n>=2)
{
u=*t;
*t=*(t+n-1);
*(t+n-1)=u;
inv(t+1,n-2));
}
}
The same function can be written using array subscripting operators as follows:相同的 function 可以使用数组下标运算符编写如下:
void inv(float t[],int n)
{
float u;
if (n>=2)
{
u=t[0];
t[0]=t[n-1];
t[n-1]=u;
inv(&t[1],n-2));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.