Home > other >  Trying to pass a value to a function that takes a pointer as argument
Trying to pass a value to a function that takes a pointer as argument

Time:10-28

Ive been searching far and wide for the answer but havent been able to come op with a viable solution. As you can see in my code beneath, I'am trying to pass the value of choice into get_user_input(char *input). I think this is what i want to do because i need the function scanf to be called inside get_user_input(char *input).

I'am fairly new to programming and have alot of trouble trying to understand pointers and references

I hope someone could help me out!

The get_user_input(char input) function

void get_user_input(char *input) {
     *input = '\0' ;


    scanf(" %c", input);
}

Trying to call scanf() from the function get_user_input(char input)

void manual_read_sensors(void) {
    while (1) {
        // Ask the user for which sensor to read.
        printf("Which sensor do you want to read?\n"
               "(i)ntensity\n"
               "(a)ngle\n"
               "(t)ime\n"
               "(s)unscreen\n"
               "(q)uit\n"
               "Enter choice: ");
        void choice = get_user_input(); <----- Trying to call 

        // Return to the main menu again.
        if (choice == 'q')
            break;

CodePudding user response:

First, your get_user_input function accepts a char * and doesn't return anything, but you are not passing anything - You should pass the address of choice like: get_user_input(&choice);. If you pass the address of choice, the get_user_input function will be able to write data to it.

Second, void choice is not a valid declaration. Although you can declare a void *choice. In this case, you should declare choice as a char: char choice;.

Third, get_user_input does not return anything and you should not try to assign the result of the function to a variable.

Fourth, *input = '\0' ; does not achieve anything as in the next step, you overwrite input with scanf.

get_user_input should look like this:

void get_user_input(char *input) {
     scanf(" %c", input);
}

manual_read_sensors should look like:

void manual_read_sensors(void) {
    char choice;
    while (1) {
        // Ask the user for which sensor to read.
        printf("Which sensor do you want to read?\n"
               "(i)ntensity\n"
               "(a)ngle\n"
               "(t)ime\n"
               "(s)unscreen\n"
               "(q)uit\n"
               "Enter choice: ");
        
        get_user_input(&choice); 

        // Return to the main menu again.
        if (choice == 'q')
            break;

CodePudding user response:

You have declared that get_user_input() function takes a char reference as its argument. But when this function was called from manual_read_sensors() function, there was no reference passed to get_user_input() function.

choice variable cannot be declared as type void because this is not allowed in C/C language (reference). Rather try declaring,

char choice;

Since get_user_input() function is declared as having void as return type, it cannot return any value to other function and hence, a variable cannot be assigned a value like so,

char choice = get_user_input(); //error

Since get_user_input() does not return any value, we need to use pointer variable for manipulating choice variable. But the pointer variable also requires the address of the variable to point to, that's why we need to pass the reference (or address) of choice variable to get_user_input() function. Like so,

get_user_input(&choice);
  • Related