-4

what is wrong with this code it compiles without error's but when i run it i get a bus error

#include <wchar.h>
#include <stdio.h>


int main(void)
{
char *a =  "yes";
char *b = "no";
char *c = "";

        puts ("\ntype yes or no for selection\n");
        puts ("do you like icecream\n");
        scanf("%c", &*c);

                if (*c == *a){
                puts("you win $1000000000");
                }

                if (*c == *b){
                puts("you loose $50");
                }
return 0;
}
  • 2
    Hint: char *c = ""; followed by scanf("%c", &*c); This is very basic stuff. – Mitch Wheat Nov 15 '13 at 03:50
  • 1) To input strings "yes" or "no" use `%s` format specifier. 2) To compare strings use `strcmp` library function, you cannot compare just by `==`. 3) Allocate some memory to `c` pointer. – 0xF1 Nov 15 '13 at 03:54
  • @0xF1: He could use `%c` if `c` pointed to modifiable memory. He can compare the first characters of the strings as shown, but he should cover the case where the input is neither `y` nor `n`. – Jonathan Leffler Nov 15 '13 at 03:56
  • @JonathanLeffler: Right, he can also do it that way. – 0xF1 Nov 15 '13 at 04:00
  • Please compile with all warnings and debug info (i.e. `gcc -Wall -g prog.c -o myprog`) and learn how to **use the debugger** (with `gdb myprog`) – Basile Starynkevitch Nov 15 '13 at 06:11

7 Answers7

2

There are some hints to get you started:

  1. Store user's answer as a string, rather than pointer to a char variable. So instead of using char *c, you can use char c[4].
  2. When getting user's answer, use scanf("%s", c);, because user's input will be yes or no, which is a string, rather than a character.
  3. When comparing user's answer with the defined answer, use strcmp method, because you are comparing string, and not just a character.
rcs
  • 6,713
  • 12
  • 53
  • 75
  • 5
    4. Learn how to spell "lose" :-) But don't ever do #2 in production code (education, assignments or quick'n'dirty applications are probably okay) - it opens you up to buffer overflow attacks. – paxdiablo Nov 15 '13 at 03:55
  • @paxdiablo. You mean it's better to use `fgets` as we can specify the max length? – rcs Nov 15 '13 at 04:01
  • 3
    rcs, yes, a favorite of mine: http://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921 – paxdiablo Nov 15 '13 at 04:02
2
char *c =  "";

Declares c a pointer to a zero-length string in unwritable memory. So scanf() has no bytes to write to, and couldn't if it did.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
0

You need to allocate memory for the pointer c. You are trying to scan a string. But where the string will be stored? change the following

char *c = "";

to

char c[MAX]; //you #define MAX to maximum length of the string.

While scanning for the string, use

scanf("%s",c); 

To know where the variables are allocated memory (for reference), https://stackoverflow.com/a/18479996/1814023

Community
  • 1
  • 1
0

scanf("%c", &*c);

This line attempts to store the user's input into const char *. Once you declare something like:

char *foo = "bar";

You cannot change it.

If you intend on changing the string you must allocate memory, either automatically or dynamically with malloc().

char foo[10] = "bar"; or simply char foo[10]

sherrellbc
  • 4,650
  • 9
  • 48
  • 77
0

char *a="yes";

+----------+
|    y     |     4000
+----------+ 
|    e     |     4001
+----------+
|    s     |     4002
+----------+
|    \0    |     4003
+----------+

Suppose your address starts at 4000, then this is how the charcaters will be stored in memory and 4000 will be assigned to a. The same applies for other two definitions. SO you store a empty character in c. Also a,b,c are all read only memory and you cannot modify or store data into it at runtime.

You get string input but you compare only characters. Instead you can get a character input. Be simple.

int main(void)
{
    char *a="yes";
    char *b="no";
    char *c=malloc(sizeof(char));
    puts ("\ntype yes(y) or no(n) for selection\n");
    puts ("do you like icecream\n");
    scanf("%c",c);
    if (*c == *a)  puts("you win $1000000000");
    if (*c == *b)  puts("you lose $50");
  return 0;
}


int main(void)
{
    char c;
    puts ("\ntype yes(y) or no(n) for selection\n");
    puts ("do you like icecream\n");
    scanf("%c",&c);
    if (c == 'y')  puts("you win $1000000000");
    if (c == 'n')  puts("you lose $50");
return 0;
}
Amarnath Krishnan
  • 1,253
  • 1
  • 9
  • 12
0
char *a =  "yes";

char *b = "no"; char *c = "";

Address pointed by c is at static memory area, and notes that the length of "" is just one char. So, you can malloc for c before using it!

0

If you define variable as a character (char), it can only hold a single byte (8 bits). Else you need to declare the variable as a string (%s). If you need to use characters, you can let the user enter character "y" for "yes" and character "n" for "no".

char *a =  "yes"; /*Wrong*/
char *b = "no";   /*Wrong*/

You have used pointers unnecessarily. Even without the pointers your goal can be achieved easily.

char *c = "";  /* Wrong */

You dont have to assign anything to "c" since you are using scanf() operator. So

char c;  /*would be fine*/

when the user enters one of "y" or "n", the character will get copied to the address of "c".