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

前端 未结 23 920
渐次进展
渐次进展 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:33

    If the problem is the syntax, it may be helpful to show equivalent code with template/using.

    template
    using ptr = T*;
    

    This can then be used as

    ptr bar = &foo;
    

    After that, compare the normal/C syntax with this C++ only approach. This is also useful for explaining const pointers.

提交回复
热议问题