0

I want to create a function that calls scanf() and passes the input to the main function. How would I do this?

#include <stdio.h>

void check(char **num){
        scanf("%s", num); //i want to 
}


int main(){
        char *num = NULL;

        check(&num); //initiates scanf

        printf("%s\n", num); //the inputted value from check
}

  • 1
    Where do you want the memory that will hold the string to be allocated, in `main` or in `check`? How do you want the lifetime of that memory to be managed? – David Schwartz Feb 17 '21 at 01:52
  • 1
    Are you on a POSIX system where [`scanf()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html) et al support the `%ms` modifier — if so, it will allocate the memory for you. If not, where do you propose to allocate the memory? Don't forget to [avoid buffer overflows using `scanf()`](https://stackoverflow.com/q/1621394/15168). – Jonathan Leffler Feb 17 '21 at 02:04
  • @DavidSchwartz I'd like it to be managed in main. – blackleopard Feb 17 '21 at 16:03

0 Answers0