-2

I am trying to modify code from MercuryAPI SDK for RFID reader. The program suppose to work by taking some argument from the use and print some message depend on the user's input. However, the program does not allow me to declare new variable inside the main function.

int main(int argc, char *argv[])

So, I tried to declare the variable as a global outside main function.

char c[1];
int main(int argc, char *argv[])
{
    //Code that already written from MercuryAPI SDK
    printf("\nEnter input: ");
    scanf("%c",c[0]);
    printf("\n%c", c[0]);
    if(*c == 'O'){
        printf("\nThe system is open");
    }
    else if(*c == 'X'){
        printf("\nThe system is unopen");
    }
}

The declaration is succeed, but the program terminate write after I input an argument. Are there any solution for this?

RamitaTr
  • 1
  • 2
  • 1
    Put all of the relevant information *as a text* in the body of the question. – Eugene Sh. Jul 05 '17 at 18:21
  • Please take the [tour], especially [ask]. – Yunnosch Jul 05 '17 at 18:23
  • change `char c[1]` to `char c` and change your `scanf` argument to `&c`. There's no point in declaring an array 1 item long, and `scanf` wants the address of where to write the input data. – yano Jul 05 '17 at 18:25
  • @yano *"change char c[1] to char c and change your scanf argument to &c. There's no point in declaring an array 1 item long, and scanf wants the address of where to write the input data."* Then `printf("\n%c", c[0]);` should be changed to `printf("\n%c", c);` – AGN Gazer Jul 05 '17 at 18:56
  • "Are there any solution for this?" --> enable all compiler warnings which would have indicated a problem with `scanf("%c",c[0]);` saving you (and us) time. – chux - Reinstate Monica Jul 05 '17 at 19:03
  • @AGNGazer yes, other changes would have to be made throughout the remainder of the code (assuming this is an MCVE) .. only intended to kickstart the process with a comment. – yano Jul 05 '17 at 19:10

3 Answers3

0

The scanf("%c",c[0]); should be scanf("%c",c);

  • 1
    @dlmeetei You would have been right if `c` were declared as `char c` and not as `char c[1]` – AGN Gazer Jul 05 '17 at 18:30
  • Agreed, but char c is better char c[1]; So, I thought Arashi need to update that also – dlmeetei Jul 05 '17 at 18:39
  • @AGNGazer yes, but seems some devs pass pointer-of-pointers to `scanf`!?! . Also I wonder how global `char[1]` could be NULL(as @dbush stated)!? Since it's `char[1]`, not `char*`. Agree `char[1]` is non-sense, but just stated the problem. –  Jul 05 '17 at 18:44
  • 1
    @dlmeetei Too bad you have deleted your first comment and now it is not clear what I was commenting on in my previous comment. *"char c is better char c[1]"* - It depends on the needs (in general). For this particular problem replacing `char c[1]` with `char c` is neither better nor worse. – AGN Gazer Jul 05 '17 at 18:49
  • One place that I have seen array of size one is at `struct hack`. My first comment was something like `scanf("%c", &c), not scanf("%c", c), sorry – dlmeetei Jul 05 '17 at 18:51
  • 1
    @Arashi dbush probably refers to https://stackoverflow.com/questions/1414215/initial-value-of-int-array-in-c Because the array is initialized at the global scope, its values are zeros (interpreted as NULL pointer) – AGN Gazer Jul 05 '17 at 18:54
0

The %c format specifier to scanf expects the address of a char. You're passing in c[0] which is the value of a char. Since c is global, all of its elements are initialized with the value 0. This means you're passing in a NULL pointer to scanf, which it then attempts to dereference. This invokes undefined behavior.

Change the scanf call to pass in the address of a char. Also, change c to a single char instead of a single element array:

char c;
...
scanf("%c",&c);
printf("\n%c", c);
if(c == 'O'){
    printf("\nThe system is open");
}
else if(c == 'X'){
    printf("\nThe system is unopen");
}

There's also no reason you shouldn't be able to define c inside of the main function, as long as the definition comes before any other statements.

dbush
  • 205,898
  • 23
  • 218
  • 273
-1

replace

scanf("%c",c[0]);

with

scanf("%c",c);

Also in your code, sometimes you use c[0] and sometimes *c:

printf("\n%c", c[0]);
if(*c == 'O'){
    printf("\nThe system is open");
}
else if(*c == 'X'){
    printf("\nThe system is unopen");
}

I would suggest to be consistent about this and, e.g., replace:

printf("\n%c", c[0]);

with

printf("\n%c", *c);
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45