我遇到两个错误,我不知道如何解决。
error 1:
incompatible pointer types initializing 'int *' with an expression of type 'char *'
error 2:
invalid operands to binary expression ('int *' and 'int')|
感谢您的帮助! 谢谢!
void play(int *boom_number, char *players)
{
int y;
char temp;
int *sorting_pointer = &players[LENGTH-1];
int current_index=0;
while (sorting_pointer!=players[0])
{
int position=((&sorting_pointer-&players[0])/sizeof(int));
current_index=boom_number%position
char temp[]=players[current_index]
for (y=current_index; y<position;y++)
{
players[y]=players[y+1]
}
players[position]=temp
*sorting_pointer--
}
}
答案 0 :(得分:2)
您应该使用最大警告进行编译:
C语
$ clang -Weverything -c test.c
test.c:7:10: warning: incompatible pointer types initializing 'int *' with an expression of type 'char *' [-Wincompatible-pointer-types]
int *sorting_pointer = &players[LENGTH-1];
^ ~~~~~~~~~~~~~~~~~~
test.c:9:27: warning: comparison between pointer and integer ('int *' and 'int')
while (sorting_pointer!=players[0])
~~~~~~~~~~~~~~~^ ~~~~~~~~~~
test.c:11:40: error: 'int **' and 'char *' are not pointers to compatible types
int position=((&sorting_pointer-&players[0])/sizeof(int));
~~~~~~~~~~~~~~~~^~~~~~~~~~~~
test.c:12:34: error: invalid operands to binary expression ('int *' and 'int')
current_index=boom_number%position
~~~~~~~~~~~^~~~~~~~~
海湾合作委员会
$ gcc -Wall -c test.c
test.c: In function ‘play’:
test.c:7:28: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int *sorting_pointer = &players[LENGTH-1];
^
test.c:9:27: warning: comparison between pointer and integer
while (sorting_pointer!=players[0])
^~
test.c:11:40: error: invalid operands to binary - (have ‘int **’ and ‘char *’)
int position=((&sorting_pointer-&players[0])/sizeof(int));
~~~~~~~~~~~~~~~~^~~~~~~~~~~~
test.c:12:34: error: invalid operands to binary % (have ‘int *’ and ‘int’)
current_index=boom_number%position
^
test.c:13:9: error: expected ‘;’ before ‘char’
char temp[]=players[current_index]
^~~~
test.c:8:9: warning: variable ‘current_index’ set but not used [-Wunused-but-set-variable]
int current_index=0;
^~~~~~~~~~~~~
test.c:6:10: warning: unused variable ‘temp’ [-Wunused-variable]
char temp;
^~~~
test.c:5:9: warning: unused variable ‘y’ [-Wunused-variable]
int y;
至于纠正错误...
这里:
void play(int *boom_number, char *players)
{
...
int *sorting_pointer = &players[LENGTH - 1];
int position = ((&sorting_pointer - &players[0]) / sizeof(int));
...
}
您可能打算做类似的事情:
void play(int *boom_number, char *players)
{
...
char *sorting_pointer = &players[LENGTH - 1];
size_t length = sorting_pointer - &players[0];
...
}
答案 1 :(得分:0)
尽管int和char是兼容类型,并且可以从int隐式转换为char,反之,即使存储类型具有指针,指针也没有任何隐式转换。考虑:
//int* c = &b; //doesn't work, because &b is char *
//char* d = &a; // doesn't work, a is int *
int* c = (int*)& b; //compiled
char* d = (char*)& d; //compiled
cout << *c << " " << *d << endl;
不幸的是,显式C转换在这些指针中导致未定义的行为,因此我们得到了不良的输出: 9-858993567 B
在这种情况下,C ++转换更为可靠,因此static_cast而不是C转换会导致编译错误
int * c = static_cast<int*>(&a); // error, invalid type conversion
char * d = static_cast<char*>(&b); // error, invalid type conversion
只有reinterpret_cast允许您执行此操作。 因此,您不能在指针中存储不是指针类型的值。