0

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?

dbush
  • 205,898
  • 23
  • 218
  • 273

1 Answers1

0

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.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • This is a great explanation. Thank you – jake edwards Jun 04 '20 at 21:49
  • "you then use iPtr but format it as %d, making the same type error in reverse. " --> Not quite the reverse. `iPtr = (int*)asciiValue;` is somewhat OK being a conversion of `int` to a pointer. `printf("%d\n",Ptr);` has no conversion, just UB of passing a pointer (which may differ in size and passing mechanism) when an `int` is expected. – chux - Reinstate Monica Jun 04 '20 at 21:49
  • I am still confused why the program is running in my IDE if there are so many errors. Any ideas why? – jake edwards Jun 04 '20 at 21:55
  • Undefined behavior is undefined. Anything can happen, including working the way you had expected it to. – Christian Gibbons Jun 04 '20 at 21:59
  • Note that the first two are *warnings*. C is pretty forgiving about type errors, and in the code provided they tend to cancel out. The argument passed to `CharacterScan` isn't really used, exactly. The conversion of `int` to `int*` doesn't hurt anything. Formatting the `int*` with `%d` is undefined behavior, but undefined behavior sometimes means working ok. It's like Forrest Gump's box of chocolates -- ya never know what ya gonna get. – Fred Larson Jun 04 '20 at 22:00