• 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. gpt最大分区容量_[电脑知识]GUID(GPT)磁盘全局唯一分区表详解
  2. CentOS安装网络代理软件
  3. PyQt5 笔记5 -- 消息框(QMessageBox)
  4. ci框架 乱码 mysql_mysql容器乱码问题
  5. 大数据没用?!张小龙:我们很少看统计数据!
  6. fetch的基本用法
  7. python 进度条_6种酷炫Python运行进度条
  8. 05-树9 Huffman Codes (30 分)
  9. Hierarchical voxel block hashing for effiecient integration of depth images
  10. ios12完美深色模式插件_那些好玩的插件 iOS 12(十七)
  11. 中班音乐会跳舞的机器人_中班音乐律动机器人
  12. 想进外企你应该知道的七大基本面试知识
  13. 基于深度学习的视频修复算法
  14. 小说作者推荐:漫漫何其多合集
  15. 所有API接口分类,淘宝/天猫API、1688API、拼多多API、京东API、各大电商API接口及测试平台
  16. “橄榄型”人口结构制约消费增长
  17. 入门行人重识别 尝试跑(郑哲东 简单行人重识别代码到88%准确率)过程
  18. 计算机论文致谢词范文500字,论文致谢词范文500字(精选5篇)
  19. facetime 来电提醒_从命令行打开FaceTime调用
  20. 公司章程绝对记载事项有哪些

热门文章

  1. QQ浏览器是如何提升搜索相关性的?
  2. 计算机协会游园活动方案,大学计算机协会演讲比赛活动策划方案
  3. C语言将三个整数进行排序
  4. Android Q Beta 正式发布 | 精于形,安于内
  5. Unity3D灯光详解
  6. 算法导论-3.递归部分习题选
  7. DISTRIB TRAN xxxxx.xxxx.xx.x.xx
  8. 【每日一读】Large Scale Network Embedding: A Separable Approach
  9. 强推win10损害用户利益?这锅微软不背
  10. VuePress自动化部署到GitHub服务器