C/C++ int[] 与 int*(指针与数组表示法).有什么区别?

时间:2023-02-13
本文介绍了C/C++ int[] 与 int*(指针与数组表示法).有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我知道 C 中的数组只是指向顺序存储数据的指针.但是什么区别意味着符号 [] 和 * 的区别.我的意思是在所有可能的使用上下文中.例如:

char c[] = "test";

如果您在函数体中提供此指令,它将在堆栈上分配字符串

char* c = "test";

将指向一个数据(只读)段.

您能否在所有用法上下文中列出这两种符号之间的所有差异以形成清晰的总体视图.

解决方案

根据C99标准:

<块引用>

一个数组类型描述了一个连续分配的非空集合具有特定成员对象类型的对象,称为元素类型.

<块引用>

  1. 数组类型的特征在于它们的元素类型和数组中元素的数量.数组类型被称为派生自其元素类型,如果其元素类型为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.

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