C/C++ int[] vs int* (pointers vs. array notation). What is the difference?(C/C++ int[] 与 int*(指针与数组表示法).有什么区别?)
问题描述
我知道 C 中的数组只是指向顺序存储数据的指针.但是什么区别意味着符号 [] 和 * 的区别.我的意思是在所有可能的使用上下文中.例如:
char c[] = "test";
如果您在函数体中提供此指令,它将在堆栈上分配字符串
char* c = "test";
将指向一个数据(只读)段.
您能否在所有用法上下文中列出这两种符号之间的所有差异以形成清晰的总体视图.
根据C99标准:
<块引用>一个数组类型描述了一个连续分配的非空集合具有特定成员对象类型的对象,称为元素类型.
<块引用>
- 数组类型的特征在于它们的元素类型和数组中元素的数量.数组类型被称为派生自其元素类型,如果其元素类型为
T
,则数组类型有时被称为T
的数组.数组的构建来自元素类型的类型称为数组类型派生.
<块引用>
指针类型可以派生自函数类型、对象类型或不完整的类型,称为引用类型.指针类型描述一个对象,其值提供对实体的引用引用的类型.从引用类型 T
派生的指针类型有时被称为 指向 T
的指针.指针的构造来自引用类型的类型称为指针类型派生.
根据标准声明...
char s[] = "abc", t[3] = "abc";char s[] = { 'a', 'b', 'c', ' ' }, t[] = { 'a', 'b', 'c' };
...完全相同.数组的内容是可修改的.另一方面,声明……
const char *p = "abc";
…将 p 的类型定义为 指向常量 char
的指针,并将其初始化为指向一个类型为 char的常量数组
的对象code>(在 C++ 中),长度为 4,其元素用字符串字面量初始化.如果尝试使用 p
修改数组的内容,则行为未定义.
根据6.3.2.1 数组下标解引用和数组下标是一样的:
<块引用>下标运算符[]
的定义是E1[E2]
是等同于 (*((E1)+(E2)))
.
数组与指针的区别在于:
- 指针后面没有关于内存大小的信息(没有可移植的方法来获取它)
- 无法构造不完整类型的数组
- 指针类型可能源自不完整的类型
- 一个指针可以定义一个递归结构(这个是前两个的结果)
有关该主题的更多有用信息,请访问 http://www.cplusplus.com/forum/articles/9/
I know that arrays in C are just pointers to sequentially stored data. But what differences imply the difference in notation [] and *. I mean in ALL possible usage context. For example:
char c[] = "test";
if you provide this instruction in a function body it will allocate the string on a stack while
char* c = "test";
will point to a data (readonly) segment.
Can you list all the differences between these two notations in ALL usage contexts to form a clear general view.
According to the C99 standard:
An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type.
- Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is
T
, the array type is sometimes called array ofT
. The construction of an array type from an element type is called array type derivation.
A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type
T
is sometimes referred to as a pointer toT
. The construction of a pointer type from a referenced type is called pointer type derivation.
According to the standard declarations…
char s[] = "abc", t[3] = "abc";
char s[] = { 'a', 'b', 'c', ' ' }, t[] = { 'a', 'b', 'c' };
…are identical. The contents of the arrays are modifiable. On the other hand, the declaration…
const char *p = "abc";
…defines p with the type as pointer to constant char
and initializes it to point to an object with type constant array of char
(in C++) with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p
to modify the contents of the array, the behavior is undefined.
According to 6.3.2.1 Array subscripting dereferencing and array subscripting are identical:
The definition of the subscript operator
[]
is thatE1[E2]
is identical to(*((E1)+(E2)))
.
The differences of arrays vs. pointers are:
- pointer has no information of the memory size behind it (there is no portable way to get it)
- an array of incomplete type cannot be constructed
- a pointer type may be derived from a an incomplete type
- a pointer can define a recursive structure (this one is the consequence of the previous two)
More helpful information on the subject can be found at http://www.cplusplus.com/forum/articles/9/
这篇关于C/C++ int[] 与 int*(指针与数组表示法).有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C/C++ int[] 与 int*(指针与数组表示法).有什么区别
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01