3

When I press a key(integer) on my keyboard. It does something like:

 gchar *keypressed;
 keypressed=gdk_keyval_name (event->keyval);
 printf("The KeyEvent is: %s\n", keypressed); // Till here it is fine    

I get segmentation fault when I do this:

char ch;
sprintf(ch, "%s\n", keypressed);   
printf("The NewKeyEvent is: %s\n",ch);

I need to convert it as I am going to use the value in a switch case. Without converting it is not possible.

djgharphalia07
  • 227
  • 3
  • 14

2 Answers2

2

gchar is a typedef alias of char, so the problem is not with the type conversion. You need to allocate some space for the sprintf buffer.

Currently, you are passing a single uninitialized char to where a pointer should be. You should get a warning from the compiler, in addition to a segfault caused by undefined behavior.

To fix this, make an array of chars and pass it to sprintf instead:

char ch[10];
sprintf(ch, "%8s\n", keypressed);   
printf("The NewKeyEvent is: %s\n", c);

Note the limit of 8 in the sprintf. This is because ch is of size 10, and we need two extra spots for '\n' and '\0'.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 2
    "I need to convert the it as I am going to use the value in a switch case. Without converting it is not possible." This wouldn't help you with the switch, though, because you cannot switch on a C string. You should switch on `event->keyval` itself, which has an integral type, and can be used as a `switch` expression. – Sergey Kalinichenko Sep 12 '15 at 09:49
  • I have converted using *atoi* and it is working fine now... event->keyval will be used soon :) – djgharphalia07 Sep 12 '15 at 12:56
  • When I do press 2 from the keyboard above the letters. The event->keyval *takes* it as a char value of decimel number 50. What should I do? – djgharphalia07 Sep 16 '15 at 07:06
  • @djgharphalia07 Isn't that what it supposed to do? Decimal number 50 corresponds to character `'2'` in [ASCII](http://www.asciitable.com/). – Sergey Kalinichenko Sep 16 '15 at 09:07
  • Yes! I know. I mean to say if I use *event->keyval* . Here too I have to convert the values. isn't it? So same thing. – djgharphalia07 Sep 16 '15 at 09:14
  • 1
    @djgharphalia07 That depends on what you wish to do with it. If you write `switch(event->keyval) { case '0': ... break; case '1': ... break; case '2': ... break; ... default: }` (note the single quotes around digits) you would switch on the numeric value of the digit, not on its code. – Sergey Kalinichenko Sep 16 '15 at 09:20
  • I faced errors when I tried this the 1st time infact :( – djgharphalia07 Sep 16 '15 at 09:48
1

If you want only a single character from the keypressed string there are two ways:

  1. Use array indexing, this way you can access any character in the string:

    char ch = keypressed[x];  // Where `x` is the character number you want
    

    Remember that array indexing starts from zero, so the first character in the string is number 0.

  2. If you want the first character, you can also use the dereference operator

    char ch = *keypressed;
    

    This equivalent to

    char ch = keypressed[0];
    
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621