1
int main(int argc, char* argv[]) {
    char* string; //local variable for a character
    string = (char*)malloc(sizeof(char));
    char* yes = "yes";

    printf("Do you want to play (y/n)?");
    scanf_s("%s", string);
    if (strcmp(yes, string) == 0) {
        printf("Hello welcome...");
}

Above is my code. Essentially, I want to make a loop asking for the user to input yes, y to continue; n, no to discontinue. But I simplify it for the sake of simple codes. The program just output the question, I press enter yes and enter then it stops. I cant figure out a way to do it using array syntax( char string[] way, although array and malloc are basically the same) so I use pointer and malloc instead. I'm going mad because this is bugging me so much. The practice assignment only asks to input character 'y' 'n' using %c but i want to do it the %s. Really appreciate any help, im really stuck now. Thank you so much

1 Answers1

0

Your code has two significant problems. First, if you want to input a string of characters, your malloc call needs to allocate space for more than one character; you should allocate the maximum number of characters you think the user's input will contain plus one - strings in C have a zero (nul) character at the end, to mark the end of the string).

Second, when you use the scanf_s function to read in a string (using the %s format specifier, as you have done), then you need to add additional parameters (the size of the target string buffer) after each string argument.

Here's a modified version of your code with these (and a few other) corrections:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    char* string = malloc(5); //local variable for a character string (up to 5 chars)
    char* yes = "yes";
    printf("Do you want to play (y/n)?");
    scanf_s("%s", string, 5);
    if (strcmp(yes, string) == 0) {
        printf("Hello welcome...");
    }
    free(string); // Don't forget to release the memory!
    return 0; // Conventionally, return 0 for success or non-zero on error
}

Note 1: Each and every call to malloc (or calloc) should be paired with a call to free to release the allocated memory, or you will end up with memory leaks.

Note 2: Please read this post: Do I cast the result of malloc?

Note 3: Although most (all?) C compilers will not insist on it, it is good practice to explicitly add a return 0; (for success) statement at the end of the main function.

Please feel free to ask for any further clarification and/or explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • bro, if this is reddit ill give u a gold medal. ill ask some questions real soon after this. just checking your explanations and hyperlinks out! thanks tons – Đằng Long Apr 26 '20 at 12:32
  • No problem! This is (IMHO) a *far* better place than Rabbit (or whatever it's called) .‎ – Adrian Mole Apr 26 '20 at 12:34
  • hey Adrian, for the casting result of malloc, my professor says that since malloc return void, it should be casted the type of whatever the pointer it is allocating for. So does that mean my prof is wrong? – Đằng Long Apr 26 '20 at 12:39
  • @ĐằngLong The first answer in the link I gave explains why (in **C**) you don't need to cast a `void*` pointer; however, in **C++** you *must* cast it - maybe your professor is a 'dual-language' programmer (as many of us are) and sometimes gets confused between the two languages? – Adrian Mole Apr 26 '20 at 12:42
  • cool @Adrian, Ill double check with him in the next ZOOM meeting. COVID hinders alot of my study :( again thanks a tons for this. sometimes the answer is easy yet so helpful but theres no one around to ask. – Đằng Long Apr 26 '20 at 12:53