c Value output using language pointer function.

Asked 2 years ago, Updated 2 years ago, 43 views

An empty function in the code below

I want to print out the number 15 using printf.

How do we make a function with a pointer? At this point, the main part is untouched.

"Code"

int set_to_five(){
}

int main(){
    int foo = 0;
    int bar = 0;
    int glorp = 0;
    printf("%d", foo+7*bar+2*glorp);
    return  0;
}

"Code"

c

2022-09-22 19:27

1 Answers

First of all, I can't.

This is because the function set_to_five() does not run in the main function.

So if you think about the alternative,


void set_to_five(int* a){
    *a=15;
}

int main(){
    int foo = 0;
    int bar = 0;
    int glorp = 0;

    set_to_five(&foo);

    printf("%d", foo+7*bar+2*glorp);
    return  0;
}

But I made a function with a pointer.

(bar and glorp variables are not used.)

We changed the value of foo to 15 by executing the set_to_five function in the middle.


2022-09-22 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.