简体   繁体   中英

I am very confused with the concept of using pointers in c

I think I have misconceptions in my understanding on pointers?

Based on my understanding, int * twenties means that twenties is a pointer to an int .

So for *twenties = dollars/20; , the *twenties here refers to the value of the pointer?

In pay_amount(money, &twenties, &tens, &fives, &ones); , the pointer twenties is storing the address of &twenties in the function of pay_amount ? Wouldn't the twenties in printf("$20 bills: %d\n", twenties); print out the address instead of the value?

#include <stdio.h>

void pay_amount(int dollars, int *twenties, int *tens, int *fives, int *ones);

int main(void) {

    int money = 0, twenties, tens, fives, ones;

    printf("Enter a dollar amount: ");
    scanf("%d", &money);
    pay_amount(money, &twenties, &tens, &fives, &ones);

    printf("$20 bills: %d\n", twenties); // why isnt not *twenties??? I got an error if I put *twenties
    printf("$10 bills: %d\n", tens);
    printf(" $5 bills: %d\n", fives);
    printf(" $1 bills: %d\n", ones);

    return 0;
}

void pay_amount(int dollars, int *twenties, int *tens, int *fives, int *ones) {

    *twenties = dollars / 20;
    dollars -= *twenties * 20;
    *tens = dollars / 10;
    dollars -= *tens * 10;
    *fives = dollars / 5;
    dollars -= *fives * 5;
    *ones = dollars;
}

No, the twenties in the main() and the twenties in the pay_amount function are different types.

While calling the pay_amount() from main() , you're passing the address of twenties of main() , and storing that in twenties (which is local to the block scope of the function) of pay_amount .

The type of twenties in main() is int , the type of twenties in pay_amount is int * .

If you wish, you can use two different variable names altogether.

So, in main() , twenties is an int , and a print statement like

 printf("$20 bills: %d\n", twenties);

is correct. Point to note, if you want to print the twenties inside the pay_amount() function call, you have to use *twenties , as you'd have expected, as that one is of type int * .

So, if you declare two variables as:

int data_holder;
int* pointer;

And assign values to them as:

data_holder = 10;
pointer = &data_holder;

What happens is this - compiler allocates memory for data_holder as well as pointer . Now, when you store 10 in data_holder and use & to get its address and store it in pointer , a link is created. To access the value 10 - you may either use data_holder or de-reference pointer .

EDIT : When we use * during declaration of a pointer variable, it tells the compiler to treat the variable differently. When we use * again for that declared pointer, we are essentially de-referencing the pointer - accessing the value stored at the memory location it is pointing to.

More concretely;

printf("The value of data_holder is %d", data_holder);
printf("The value of data_holder is %d", *pointer);

Similarly, the value 10 can be modified in two ways;

data_holder = 200;       //The value is changed from 10 to 200
*pointer = 300;          //The value is changed from 200 to 300

Inside your function, twenties etc. are pointers, whereas in main() the twenties etc. are integers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM