问题描述
我知道 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
Tis 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*(指针与数组表示法).有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)