• Variable vs. Pointer

int foo;
int *foo_ptr = &foo;

Grammatrically speaking, there is no such thing as a “pointer variable”: all variables are the same.

There are, however, variables with different types. foo's type is int. foo_ptr's type is int *.

The point of that is that “the pointer is not the variable!”. The pointer to foo is the contents of foo_ptr.

The pointer has a type, too, by the way. Its type is int. Thus it is an "int pointer" (a pointer to int : int *ptr).

An int **'s type is int * (it points to a pointer to int). The use of pointers to pointers is called multiple indirection.

  • Declaration syntax

  1. Multiple variables

    int *ptr_a, not_ptr_b;
    

    In this way, not_ptr_b is not a pointer. * operator belongs to the variable and not to the type, this is something that could be quite confusing.

  2. d

  • Initialization

A pointer needs to be initialized before its used if you don’t want it to use some other objects address.

When initializaing a pointer you do it with the new operator, this is called dynamic allocation as it is done in runtime and not compile time.

char * c = new char;   // dynamic alloc (one char)

A common mistake is thinking that you can use uninitialized pointers.

int *ip;
*ip = 12;   // Gives error!

When you dynamically assign memory to objects you should be careful to return the memory by delete. If you don’t return memory it becomes a spaceleak in your program since no other object can now occupy the space you used.

delete c;    // deletes space pointer used
  • Assignment and pointers

foo_ptr = 42;

This is a wrong way. Compiler usually warn when you try to assign an int to a pointer variable. gcc will say “warning: initialization makes pointer from integer without a cast”.

  • Dereferencing (dereference operator *)

int bar = *foo_ptr;
*foo_ptr = 42; // store operation

The dereference operator * looks up the value that exists at an address.

  • Array vs. Pointers

int array[] = {45, 67, 89};
  1. decaying

    One neat feature of C is that, in most places, when you use the name array again, you will actually be using a pointer to its first element (in C temrs, &array[0]), this is called “decaying”: the array decays to a pointer.

    Decaying is an implicit &; array == &array == &array[0]. In English, these expressions read “array”, “pointer to array”, and “pointer to the first element of array” , the subscript operator [] has higher precedence than the address-of operator.

  2. Difference between array and pointer

    1. assigning to the name array
    2. passing it to the sizeof operator
  3. points

    1. passing an array to function

      When you pass an array as an argument to a function, you really pass a pointer to the array’s first element, because the array decays to a pointer. You can only give cout (which like printf in C) the pointer, not the whole array. This is why cout has no way to print an array: It would need you to tell it the type of what’s in the array and how many elements there are, and both the format string and the list of arguments would quickly get confusing.

    2. d

    3. d

  • Why do we need pointers

There is no such thing as C++ pointers. C++ is just a brand name. There are pointers. They always exist regardless of whether you’re using C++ or not. C++ grudgingly(勉强) exposes them to you; some high-level languages hide them from you. But they are always there, covertly or overtly.

Pointers Are Just Numbers.

In addition to being just number, you can also think of a pointer as being the address, or the label, of a specific byte in computer memory.

Since the pointers are the memory locations, why can’t we just remember the addresses of all the data we need, directly?

Well – honestly – until the 1980s, that is actually the way we all used to write code. When you only have 256 bytes of memory available, that’s not a lot of memory to keep track of. So we didn’t need pointers so much. You just had to remember the address of where you put evernthing, and put it there ***directly***.

But as memory got bigger, and programs got more complex, we needed some way to abstract that complexity. So now we prefer to store data ***indirectly***, using indrect addressing.

Nowadays, your cell phone has several hundred billion bytes lay around. So, code uses pointers, and pointers to pointers, and pointers to pointers to variables, and pointers to pointers to arrays of functions, and on and on like that, to keep all those code responsibilities clear.

Computers have always been very good at addressing bytes indirectly.

The assembly language of all modern CPUs lets you access data indirectly.

So, to really grok pointers, I suggest that you ignore C++ and other high level languages, and start doing a little programming in assembly language.

I know that learning assembly sounds like a huge detour, but I promise that the time you spend won’t be wasted.

Assembly programming is a pointer party, all day long.

Assembly programming is not safe, per se. There are no adults around anymore, to keep you from jabbing your own eye out with a pointer. C++ puts pointers on a high cabinet and says, clam down children, I’s the grown-up here and I will manage all the details of your pointers for you.

But assemblyy language is fast and fun as hell. When you feel the need… the need for speed… you’ll want to start manipulating pointers directly from assembly language.

  • From Wikipedia

From wikipedia, a pointer in computer science is an object that stores a memory address. This can be that of another located in computer memory, or in some cases, that of memory-mapped computer hardware.

A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.

  1. History

    In 1955, Soviet computer scientist Kateryna Yushchenko invented the Address programming language that made possible indirect addressing and addresses of the highest rank - analogous to pointers. This language was widely used on the Soviet Union computers. However, it was unknown outside the Soviet Union and usually Harold Lawson is credited with the invention, in 1964, of the pointer.

  2. A Pointer is a kind of reference

    reference :

    In computer science, a reference is a value that enables a program to indirectly access a particular datum, such as a variable’s value or a record, in the computer’s memory or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference.

    datum :

    Datum is, from its Latin origin, a singular form of “data”. From “data” in wikipedia, data are characteristics or information ,usually numberical, that are collected through observation. In a more technical sense, data is a set of values of qualitative or quantitative variables about one or more persons or objects, while a datum (singular of data) is a single value of a single variable.

    A data primitive is any datum that can be read from or written to computer memory using one memory access(for instance, both a byte and a word are primitives).

    A data aggregate is a group of primitives that are logically contiguouse in memory and that are viewed collectively as one datum. When an aggregate is entirely composed of the same type of primitive, the aggregate may be called an array.

    A byte is the smallest primitive; each memory address specifies a different byte. The memory address of the initial byte of a datum is considered the memory address (or base memory address) of the entire datum.

  • Reference

  1. cplusplus.com
  2. Everything you need to know about pointers in C
  3. How do I learn pointers in C/C++?
  4. How can I understand C++ pointers inside and out? Even after 3 semesters of C++ programming (including data structures), I still don’t fully understand how pointers work.

理解Pointers In C++:第一重相关推荐

  1. 2022/1/23(每周总结)

    刚开始放假有点懈怠了,爱睡懒觉,下周要尽量早起了 p1182 luogu 二分的一道题,可以说是个模板了,但我不会...这是做的第二道这样的题,l就是数组中的最大值,r就是数组的和,然后二分枚举每组的 ...

  2. [编程神域 C语言浮游塔 第⑤期]内存地址——指针

    前言 嗨嗨,这里是渡枫,欢迎阅读第五期的C语言浮游塔. 在正式学习指针之前,我们先提出几个问题. 什么是指针? 什么决定了数据的长度? 指针是否有类型? &a是指针吗? 一:指针就是一个变量的 ...

  3. 关于Two pointers的个人理解

    刚刚在刷题的时候接触到了一道题,题的大意是给出一个递增的数字序列,并给出一个m,要求找到a,b两个数字,且和为m,并且a<b: 在示例中,给出了三种思路,二分.hash值.以及two point ...

  4. pytorch lstm crf 代码理解 重点

    好久没有写博客了,这一次就将最近看的pytorch 教程中的lstm+crf的一些心得与困惑记录下来. 原文 PyTorch Tutorials 参考了很多其他大神的博客,https://blog.c ...

  5. pytorch lstm crf 代码理解

    好久没有写博客了,这一次就将最近看的pytorch 教程中的lstm+crf的一些心得与困惑记录下来. 原文 PyTorch Tutorials 参考了很多其他大神的博客,https://blog.c ...

  6. 深入理解 wpa_supplicant(四)

    本文为<深入理解Android Wi-Fi.NFC和GPS卷>读书笔记,Android源码为Android 5.1 android-5.1/external/wpa_supplicant_ ...

  7. 深入理解 wpa_supplicant(三)

    本文为<深入理解Android Wi-Fi.NFC和GPS卷>读书笔记,Android源码为Android 5.1 android-5.1/external/wpa_supplicant_ ...

  8. 深入理解 wpa_supplicant(一)

    本文为<深入理解Android Wi-Fi.NFC和GPS卷>读书笔记,Android源码为Android 5.1 wpa_supplicant 是一个开源软件项目,它实现了 Statio ...

  9. C++ 智能指针std::shared_ptr简单使用和理解

    参考:https://blog.csdn.net/u011068702/article/details/83692838 1  智能指针std::shared_ptr相关知识和如何使用 我们这里先说下 ...

最新文章

  1. jQuery.extend 函数详解
  2. 课堂练习--最大子数组和 环
  3. Android 日夜间切换Demo
  4. 使用guava带来的方便
  5. python指数运算是不是有问题_为什么在Python 3中复指数运算如此之快?
  6. 面向对象(程序员最呆的地方,一切皆是对象)
  7. 漫画:优秀程序员的必备特质有哪些?
  8. 记一次去掉中间的某次merge代码
  9. 软件测试工程师简历项目经验怎么写?一千个软件测试简历范文模板
  10. DLNA和UPnP是什么关系?通俗解释
  11. IO、NIO和Netty
  12. 搜索不到网络计算机和设备,电脑搜不到无线网络原因分析和对应解决办法
  13. Activity子流程——调用子流程
  14. 进一步学习操作系统 - 哈工大李治军老师 - 学习笔记 L13L14L15
  15. Django新手十个开发指导
  16. ntp 服务 --Centos
  17. 51CTO专访人人网黄晶:WEB开发需要随需应变(2)
  18. Mysql数据库备份(一)
  19. C++ Test 关于Read Symbols报错的解决方法(适用于Windows 10)
  20. edge浏览器 F12中文换成英文

热门文章

  1. 三、nginx服务的nginx.conf的参数配置解析
  2. 干货|一网打尽,100个高价值资料库免费使用!还不快为孩子们收藏?|方格教育
  3. 【CUDA】C++实现warpaffine仿射变换及其逆变换
  4. java 依赖的项目报错_关于eclipse的maven项目Java Build Path中maven依赖报错问题
  5. 汇编程序:通过查表方法实现0-9的平方
  6. C#开发微信门户及应用(46)-基于Bootstrap的微信门户应用管理系统功能介绍
  7. 计算机绘图图框实验报告,制图基础及计算机绘图实验报告.doc
  8. MaxEnt软件的安装
  9. 【CSS】grid和flex
  10. 程序员如何提高自己的逻辑思维和技术水平