How to explain C pointers (declaration vs. unary operators) to a beginner?

前端 未结 23 853
渐次进展
渐次进展 2020-11-30 18:08

I have had the recent pleasure to explain pointers to a C programming beginner and stumbled upon the following difficulty. It might not seem like an issue at all if you alre

23条回答
  •  一生所求
    2020-11-30 18:14

    I'd rather read it as the first * apply to int more than bar.

    int  foo = 1;           // foo is an integer (int) with the value 1
    int* bar = &foo;        // bar is a pointer on an integer (int*). it points on foo. 
                            // bar value is foo address
                            // *bar value is foo value = 1
    
    printf("%p\n", &foo);   // print the address of foo
    printf("%p\n", bar);    // print the address of foo
    printf("%i\n", foo);    // print foo value
    printf("%i\n", *bar);   // print foo value
    

提交回复
热议问题