C / C ++ INT [] VS为int *(指针与数组符号)。有什么不同? [英] C/C++ int[] vs int* (pointers vs. array notation). What is the difference?

查看:166
本文介绍了C / C ++ INT [] VS为int *(指针与数组符号)。有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在C数组仅会指向顺序存储的数据。但什么区别表明,[]和*在符号的差异。我所指的是所有可能的使用环境。
例如:

 字符C [] =测试;

如果你提供一个函数体该指令会分配一个堆栈上的字符串,而

 的char * C =测试;

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

您可以列出所有在所有使用情境这两个符号之间的差异,形成一个清晰的普遍看法。


解决方案

据C99标准:


  

这是数组类型描述了连续分配非空集
  与特定的成员对象类型,称为元件对象
  type.36)数组类型的特征在于其元件的类型和由
  数组中元素的数目。阵列型被认为是
  从它的元素类型派生,并且如果它的元素类型是T,阵列
  型有时也被称为''T阵'。阵列的结构
  从元素类型类型被称为'数组类型派生''。




  

一个指针类型可以从函数类型,对象类型中导出,或
  不完全类型,称为引用类型。指针类型
  介绍一个对象,其价值提供了一个实体的引用
  被引用的类型。从引用类型T衍生的指针类型
  有时也被称为'指针至T'。指针的结构
  从引用类型类型被称为'指针类型推导''。


根据标准声明

 个char [] =ABC,T [3] =ABC;
个char [] = {'A​​','B','C','\\ 0'},T [] = {'A​​','B','C'};

是相同的。的数组的内容是可修改的。另一方面,声明
为const char * p =ABC; 限定P型'指针定焦',并初始化它指向同类型''字符常量数组的对象''(用C ++),长度为4的元素用字符串文字进行初始化。如果试图使用p来修改数组的内容,该行为是未定义

6.3.2.1数组下标取消引用和数组下标是相同的:


  

下标运算符[]的定义是,E1 [E2]是
  相同(*((E1)+(E2)))。


VS数组指针的区别是:


  • 指针有它背后没有内存大小的信息(有没有可移植的方式来得到它)

  • 无法构造不完全类型的数组

  • 指针类型,可以从一个不完整的类型
  • 导出
  • 指针可以定义一个递归结构(这个人是previous两个后果)

这些链接可能对对象很有用:

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.36) 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 called ‘‘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', '\0' }, 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 type ‘‘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)

These links may be useful to the subject:

这篇关于C / C ++ INT [] VS为int *(指针与数组符号)。有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆