Search code examples
cpointersparameters

Question regarding pointers and parameters, from Chapter Seven Exercises, in "Learn C on the Mac, Second Edition"


The reason for this posting is because I'm having difficulty understanding a couple of questions in the "Chapter Seven Exercises", from "Learn C on the Mac, Second Edition".

Part a:

//  the following code is part a, of question 1
void add_one(int *my_var);

int main(int argc, const char * argv[]) {
    
    int num, i;
    
    num = 5;
    
    for (i = 0; i < 20; i++) {
        add_one(&num);
//        printf("Num equals: %d\n", num);
    }
    
    printf("Final value is %d.\n", num);
    
    return 0;
}

void add_one(int *my_var)
{
    (*my_var) ++;
}

Part b:

//  the following code is part b, of question 1
int g_number;
int multiply_it(int my_var);

int main(int argc, const char * argv[]) {
    
    int i, g_number = 2;
    
    for (i = 1; i <= 2; i++) {
        g_number *= multiply_it(g_number);
//        printf("Global number equals: %d\n", g_number);
    }
    
    printf("Final value is %d.\n", g_number);
    
    return 0;
}

int multiply_it(int my_var) {
    return (my_var * g_number);
}

Part c:

//  the following code is part c, of question 1

int g_number;
int double_it(int my_var);

int main(int argc, const char * argv[]) {
    
    int i;
    
    g_number = 1;
    
    for (i = 1; i <= 10; i++) {
        g_number *= double_it(g_number);
    }
    
    printf("Final value is %d.\n", g_number);
    
    return 0;
}

int double_it(int my_var) {
    return 2 * my_var;
}

In the back of the book, they give the final value, for part b, as "512"; for part c, as "1024".

I understand how they arrived at the answer, for part a, but I'm having difficulty understanding the answers they got for parts b and c.

The three different parts are presented, in the textbook, as code fragments, but part of the problem is each have their own main(). I'm not sure why it's presented this way, but I've tried commenting out the other main() declaration lines (except for the code within them).

When I tried combining parts a and b, part a would output the correct answer (25), but part b would output a value of "0". So, I didn't even try part c, yet. Because part c doubles part b, I'm assuming I could figure out part c, if I could figure out part b.

Is there someone who could help me understand this, or point me in the direction of where I could understand the answer?

Thanks, in advance, for your help!


Solution

  • First, global integer variables are initialized to 0 on its declaration. So, part b in your code, int g_number; will be set to 0.

    Second, g_number in function multiply_it() refers to the global variable which is 0. As a result, it'll return 0 since myvar * 0 is always 0.

    int multiply_it(int my_var) {
        return (my_var * g_number);
    }
    

    My guess is that your local variable g_number should NOT be declared. Instead, global variable should be assigned to 2.

    This should be

    int i, g_number = 2;
    

    like this

    int i;
    g_number = 2;
    

    and it prints 512 as expected.