[英]C- Why does my code run in an IDE but not a linux environment(silo)?
這是我的代碼
#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: %d\n",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*);
^
function 被聲明接受int*
類型的參數,但您傳遞的是int*
的地址,使其成為int**
。 因此,傳遞的參數類型與 function 聲明接受的類型不匹配。 類型不匹配。
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'
function _getch()
是 Windows 特定的 function,在 Linux 中沒有等效項。 您可以實現類似的東西,但在 Windows 之外沒有_getch()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.