数组与指针之间的微妙关系

编程语言中的数据类型

或许你会感到奇怪,不是要说数组和指针之间的关系吗?怎么一走来却扯到数据类型了呢?

我之所以这样安排,就是觉得有太多的人没有把指针和数组当作彼此不同的类型来看待,从而导致了很多理解上的误区。也许这样说,有些言过其实,但我本人始终认为学习一样东西,就应该从学习基本概念着手,并且学习的一开始就接受正确的概念是至关重要的。

那么在编程语言中,数据类型到底是个什么概念呢?

来看看wikipedia如何解释:

In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support basic data types of integer numbers (of varying sizes), floating-point numbers (which approximate real numbers), characters and Booleans. A data type constrains the values that an expression, such as a variable or a function, might take. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. A data type provides a set of values from which an expression (i.e. variable, function, etc.) may take its values.

根据字面意思,数据类型是用来标识分类我们在程序中使用的各种数据的,例如整数,布尔数,浮点数等。说白了,就是给了数据实际的含义,否则谁也不知道那些数据是什么意思。此外,不同的数据类型的取值范围不同,对它们可以使用的操作彼此不同,计算机对它们的存储方式也是不同的。例如int型的数据范围包括(... -2,-1,0,1,2 ...),具体的范围是由所在的机器硬件决定的。可以对int型数据进行的操作包括加减乘除法,甚至自增、自减操作。其它的数据类型的数据范围和可以对其进行的操作是不一样的。

通俗点,我们可以这样来理解:我们在现实世界中,会用整数、小数等来区分我们遇到的数字,而在计算机中用整数、浮点数来区分整数和小数。当然因为计算机存储的问题,同时也算是节约计算机的内存资源,人们又将整数分为很多不同的类型,像char,short,int,long,long long等。而同样的浮点型也分为单精度和双精度。

数据类型的属性可以归纳为三类:

  •  数值范围 
  • 可以对这些数值进行的操作,例如加减乘除等
  •  计算机存储这些数值的形式

详细可参考以下两个链接:

data type

DATA TYPES

 

C语言中的数据类型

在C语言中,将数据类型总体分为三类,分别为对象类型(object types)、函数类型(function types)以及不完全类型(incomplete types)。

我们可以具体看看标准是如何说明的,如下(ISO/IEC 9899:1999 (E) 6.2.5):

The meaning of a value stored in an object or returned by a function is determined by the type of the expression used to access it. (An identifier declared to be an object is the simplest such expression; the type is specified in the declaration of the identifier.)  Types are partitioned into object types (types that fully describe objects), function types (types that  describe  functions),  and incomplete  types (types  that  describe  objects  but  lack information needed to determine their sizes).

第一句话用来概要地解释了什么是数据类型,即数据类型决定了数据在程序中的意义。这里提到了表达式(expression)这个概念,并且指出类型是表达式的基本属性,关于表达式更详细的讨论,请参阅这篇文章《C语言基本概念之表达式》。接着C标准将类型划分为三类,即上面提到的三类。

最近发现一张归纳得非常详细的C类型图,如下:

从这张图中,我们可以看到,尽管数组和指针都属于对象类型,但是它们真真切切是彼此无关的数据类型

既然知道它们是单独的数据类型,那么什么“数组就是指针”诸如此类的言论想都不用想就可以断定是完全错误的嘛!!!

 

剪不断,理还乱的关系

既然数组和指针是两种截然不同的数据类型,那么数组就是数组,指针就是指针啊,它们之间应该没有任何关系才对嘛,这样不就天下太平了吗? 可是为什么现实中总是感觉它们彼此之间有着剪不断,理还乱的关系呢?

世界就是这样,总不是那么的完美,C语言也一样! 

究其原因,我认为是这是C语言早期一度只使用标量类型导致的。所谓标量类型的对象,可以简单地理解为一个对象只存储单独的数值,相对结构类型和数组类型的对象而言,它们存储的就不仅仅是单独的数值了,它们被称为聚合类型。

尽管C标准后期增加了可以整合使用结构体聚合类型的功能,但是依然没有支持对数组的整合使用,因此将数组赋值给另外一个数组,或者将数组作为参数传递给其它函数等手段,在C中都是不存在的。

参考链接:

数组与指针---都是"退化"惹的祸

深入理解数组与指针的区别 

指针和数组解析

Learning C with gdb

Understanding C by learning assembly

Understanding C by learning assembly (hackerschool.com)

The Ksplice Pointer Challenge

Are pointers and arrays equivalent in C?

Literal string initializer for a character array

C 語言的 decay

C Programming: What is the difference between an array and a pointer?

C: differences between char pointer and array

Array to pointer decay and passing multidimensional arrays to functions

Arrays and Pointers

C 語言新手十誡(The Ten Commandments for Newbie C Programmers)

Arrays and Pointers

What's the difference between array and pointer in C/C++?

The difference between arr and &arr

Difference between dereferencing pointer and accessing array elements

Difference between passing array and array pointer into function in C

Array Are Not Pointers

pointers and array in C

the difference between pointer and array (and variable)

Difference between C Arrays and Pointers

Is pointer a data-type or what

指针究竟是什么?是地址?还是类型?

C语言学习趣事_20_关于数组名与指针的讨论

数组名隐式转换为指针时是右值还是不可修改的左值

difference between pointer to an array and pointer to the first element of an array

数组与指针的艺术

What exactly is a C pointer if not a memory address?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值