简体   繁体   English

我对在 c 中使用指针的概念感到非常困惑

[英]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 .根据我的理解, int * twenties意味着 twenties 是指向int的指针。

So for *twenties = dollars/20;所以对于*twenties = dollars/20; , the *twenties here refers to the value of the pointer? ,这里的*twenties指的是指针的值吗?

In pay_amount(money, &twenties, &tens, &fives, &ones);pay_amount(money, &twenties, &tens, &fives, &ones); , the pointer twenties is storing the address of &twenties in the function of pay_amount ? , 指针twenties&twenties的地址存储在 pay_amount 的pay_amount中? Wouldn't the twenties in printf("$20 bills: %d\n", twenties); 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.不, main()中的twentiespay_amount twenties中的 20 是不同的类型。

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 .main()调用pay_amount() ) 时,您传递了main()twenties的地址,并将其存储在 pay_amount 的twenties (函数的块pay_amount的本地)中。

The type of twenties in main() is int , the type of twenties in pay_amount is int * . main()twenties的类型是intpay_amounttwenties的类型是int *

If you wish, you can use two different variable names altogether.如果您愿意,您可以完全使用两个不同的变量名称。

So, in main() , twenties is an int , and a print statement like因此,在main()中, twenties是一个int和一个 print 语句,例如

 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 * .需要注意的是,如果您想在pay_amount() function 调用中打印twenties ,您必须使用*twenties ,正如您所期望的那样,因为它的类型是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 .会发生什么 - 编译器为data_holderpointer分配 memory 。 Now, when you store 10 in data_holder and use & to get its address and store it in pointer , a link is created.现在,当您将 10 存储在data_holder中并使用&获取其地址并将其存储在pointer中时,将创建一个链接。 To access the value 10 - you may either use data_holder or de-reference pointer .要访问值 10 - 您可以使用data_holder取消引用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.当我们再次对声明的指针使用*时,我们实际上是在取消引用指针 - 访问存储在它指向的 memory 位置的值。

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;类似地,值 10 可以通过两种方式修改;

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.在您的 function 中, twenties等是指针,而在main()中, twenties等是整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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