简体   繁体   中英

C- Why does my code run in an IDE but not a linux environment(silo)?

This is my code

#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 ;
}

As I said, it runs fine in the IDE I am using, but it doesn't run in a Linux environment. I get the following errors:

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

I have never encountered this problem before. Does anyone know what could be the problem?

Let's take those error messages one at a time:

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*);
  ^

The function is declared to accept an argument of type int* , but your are passing the address of an int* , making it a int** . Therefore the type of the argument being passed does not match the type the function is declared to accept. Type mismatch.

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 is of type int* , and asciiValue is of type int . By casting, you have told the compiler to interpret the bit pattern of asciiValue as a pointer to int . This is not correct. later, in the printf , you then use iPtr but format it as %d , making the same type error in reverse. It happens to work out, but this is undefined behavior.

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

The function _getch() is a Windows-specific function that has no equivalent in Linux. You can implement something like it , but there is no _getch() outside of Windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM