C-为什么我的代码在IDE中运行,而不是在Linux环境(筒仓)中运行?



这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void CharacterScan(int*);
int main(void){
int* iPtr;
CharacterScan(&iPtr);

}
void CharacterScan(int* iPtr){
char ch;
int asciiValue;
do{
printf("Enter any character: n");
ch = _getch();
asciiValue=(int)ch;
iPtr = (int*)asciiValue;
printf("ASCII value of character %c is: %dn",ch,iPtr);
}while(ch != 27);
return ;
}

正如我所说,它在我使用的 IDE 中运行良好,但它不能在 Linux 环境中运行。我收到以下错误:

testchar.c: In function ‘main’:
testchar.c:19:5: warning: passing argument 1 of ‘CharacterScan’ from incompatible pointer type [enabled by default]
CharacterScan(&iPtr);
^
testchar.c:15:6: note: expected ‘int *’ but argument is of type ‘int **’
void CharacterScan(int*);
^
testchar.c: In function ‘CharacterScan’:
testchar.c:30:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
iPtr = (int*)asciiValue;
^
/tmp/cceTSMdl.o: In function `CharacterScan':
testchar.c:(.text+0x32): undefined reference to `_getch'
collect2: error: ld returned 1 exit status

我以前从未遇到过这个问题。有谁知道可能是什么问题?

让我们一次处理一条错误消息:

testchar.c: In function ‘main’:
testchar.c:19:5: warning: passing argument 1 of ‘CharacterScan’ from incompatible pointer type [enabled by default]
CharacterScan(&iPtr);
^
testchar.c:15:6: note: expected ‘int *’ but argument is of type ‘int **’
void CharacterScan(int*);
^

该函数被声明为接受类型int*的参数,但是您传递的是int*地址,使其成为int**。因此,传递的参数的类型与声明为接受的函数的类型不匹配。类型不匹配。

testchar.c: In function ‘CharacterScan’:
testchar.c:30:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
iPtr = (int*)asciiValue;
^

iPtr的类型为int*asciiValue的类型为int。通过强制转换,您已告诉编译器将asciiValue的位模式解释为指向int的指针。这是不正确的。稍后,在printf中,您可以使用iPtr但将其格式化为%d,反向产生相同的类型错误。它碰巧解决了,但这是未定义的行为。

/tmp/cceTSMdl.o: In function `CharacterScan':
testchar.c:(.text+0x32): undefined reference to `_getch'

函数_getch()是特定于Windows的函数,在Linux中没有等效功能。你可以实现类似的东西,但在Windows之外没有_getch()

最新更新