了解类型系统的重要性 ·一点个人评论(对发行量最大那本C程序设计教材) C99有关数组类型的描述 An array type describes a contiguously allocated nonempty set of objects with a particular member object type,called the element type.Array type are characterized by their element type and 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 is sometimes called "array of T
了解类型系统的重要性 • 一点个人评论(对发行量最大那本C程序设计教材) – C99有关数组类型的描述 An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. Array type are characterized by their element type and 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 is sometimes called “array of T ”. 7
了解类型系统的重要性 ,一点个人评论对发行量最大那本C程序设计教材) 没有数组类型的概念,多维数组的描述虽直观, 但不清晰 教材:f1 oat payl3]I6]定义一个f1oat型的二维数 组,第1维有3个元素,第2维有6个元素。二维数组 可看作一种特殊的一维数组,其元素又是一个数组 ● 正确的观点:对于声明float pay[3]I6],pay是 大小为3的一种数组类型的变量,该数组类型的元 素的类型是:大小为6、元素类型为f1oat的数组类型 介绍程序员自己定义数据类型和关键字typedef的 介绍太晚
了解类型系统的重要性 • 一点个人评论(对发行量最大那本C程序设计教材) – 没有数组类型的概念,多维数组的描述虽直观, 但不清晰 • 教材:float pay[3][6]定义一个float型的二维数 组,第1维有3个元素,第2维有6个元素。二维数组 可看作一种特殊的一维数组,其元素又是一个数组 • 正确的观点: 对于声明float pay[3][6],pay是 大小为3的一种数组类型的变量,该数组类型的元 素的类型是: 大小为6、元素类型为float的数组类型 – 介绍程序员自己定义数据类型和关键字typedef的 介绍太晚 8
了解类型系统的重要性 一点个人评论对发行量最大那本C程序设计教材) -例1:读者难以理解编译器就下面▣代码报告的错误 typedef int array 100]50]; void f(array a)a]10; main()int b[50][100];f(b); 该函数在Liux上用GCC编译,报告的错误如下: ●第2行: incompatible types when assigning to int50]'from type 'int' ●第3行:expected int(*)儿50]',but argument is of type 'int (*100]
了解类型系统的重要性 • 一点个人评论(对发行量最大那本C程序设计教材) – 例1: 读者难以理解编译器就下面代码报告的错误 typedef int array[100][50]; void f(array a) { a[0] = 10; } main( ) { int b[50][100]; f(b); } – 该函数在Linux上用GCC编译, 报告的错误如下: • 第2行: incompatible types when assigning to ‘int[50]’ from type ‘int’ • 第3行: expected ‘int (*)[50]’, but argument is of type ‘int (*)[100]’ 9
了解类型系统的重要性 ·一点个人评论(对发行量最大那本C程序设计教材) 解释一下指针运算符“*” ●long al10l; a-> 10 ·a和a+n分别代表数组a a+1-> 20 的第1和第n个元素的地址 。*a表示取相应地址的内容 ●*a和a0]都等于10 。*(a+1)和a1]都等于20
了解类型系统的重要性 • 一点个人评论(对发行量最大那本C程序设计教材) – 解释一下指针运算符“*” • long a[10]; • a和a+n分别代表数组a 的第1和第n个元素的地址 • *a表示取相应地址的内容 • *a和a[0]都等于10 • *(a+1)和a[1]都等于20 10 10 20 a → a+1 →
了解类型系统的重要性 ·一点个人评论(对发行量最大那本C程序设计教材) 一例2:读者难以理解编译器就下面代码报告的错误 long a[10][10]; a[0][0] main(){*(a+1)=a+1;} a0][1] 该函数在Linux上用GCC编译, a1]0 报告的错误如下: a11] incompatible types when assigning to long int10]'from ●。 type 'long int (*)[10] a[90] a91
了解类型系统的重要性 • 一点个人评论(对发行量最大那本C程序设计教材) – 例2: 读者难以理解编译器就下面代码报告的错误 long a[10][10]; main( ) { *(a + 1) = a + 1; } – 该函数在Linux上用GCC编译, 报告的错误如下: incompatible types when assigning to ‘long int[10]’ from type ‘long int (*)[10]’ … … 11 … … a[9][0] a[9][1] a[1][0] a[1][1] … … a[0][0] a[0][1] … … a →