[Rpm] C / C ++ array name and pointer depth exploration difference

   Introduction

   pointer is a feature of C / C ++ language, and the name of the array pointer there are too many similarities, even a lot of the time, the name of the array can be used as a pointer. Ever since, many programmers will be confused. Many university teachers, they also have to give an error in the process of teaching students about the C language: "The array name is a pointer." Luckily, my university teacher is one of them. Today, day after day, I engaged in the development of C / C ++ project, and has also been around so full of programmers, they are retained "is the name of the array pointer" misunderstanding.

  Presumably the root of this misunderstanding is that a domestic well-known C programming tutorial. If this article can correct many of the misconceptions about Chinese programmers array names and pointers, I would not quite satisfied. Whereby the article, the author standing in countless knowledge-hungry Chinese programmer, deeply hope domestic computer book authors who can to "in-depth exploration of" the seriousness of thinking and excellence to treat books written work I hope the market a little more effort into the author's thinking crystallized for!

   Magical array name

  See program (paper program is compiled under the WIN32 platform):
 

1. #include <iostream.h>
2. int main(int argc, char* argv[])
3. {
4.  char str[10];
5.  char *pStr = str;
6.  cout << sizeof(str) << endl;
7.  cout << sizeof(pStr) << endl;
8.  return 0;
9. }


   1, the array name is not a pointer

  we first overthrow "the array name is a pointer," saying that by contradiction.

  It proved not a pointer array name

  assumptions: a pointer to an array name;

  then: pStr and pointers are str;

  as: In WIN32 platform, the pointer length of 4;

  it: line 6 and the line 7 should be output is 4;

  actual is: line 6 outputs 10, line 7 output 4;

  so: assumption does not hold, an array name is not a pointer

   2, the array name likeness pointer

  above, we have shown that an array name does not pointers, but we look at the fifth program Row. The name of the array line program will be directly assigned to a pointer, it seemed array name and is indeed a pointer!

  We can also find examples of array names appear as pointers:

1. #include <string.h>
2. #include <iostream.h>
3. int main(int argc, char* argv[])
4. {
5.   char str1[10] = "I Love U";
6.   char str2[10];
7.   strcpy(str2,str1);
8.   cout << "string array 1: " << str1 << endl;
9.   cout << "string array 2: " << str2 << endl;
10.  return 0;
11. }

  Two parameters function prototype standard C library functions strcpy's can accept both as char type pointer, and we passed it in the call is two arrays name! Function output:

string array 1: I Love U
string array 2: I Love U

  数组名再一次显得像指针!

  既然数组名不是指针,而为什么到处都把数组名当指针用?于是乎,许多程序员得出这样的结论:数组名(主)是(谓)不是指针的指针(宾)。

  整个一魔鬼。

  揭密数组名

  现在到揭露数组名本质的时候了,先给出三个结论:

  (1)数组名的内涵在于其指代实体是一种数据结构,这种数据结构就是数组;

  (2)数组名的外延在于其可以转换为指向其指代实体的指针,而且是一个指针常量;

  (3)指向数组的指针则是另外一种变量类型(在WIN32平台下,长度为4),仅仅意味着数组的存放地址!

  1、数组名指代一种数据结构:数组

  现在可以解释为什么第1个程序第6行的输出为10的问题,根据结论1,数组名str的内涵为一种数据结构,即一个长度为10的char型数组,所以sizeof(str)的结果为这个数据结构占据的内存大小:10字节。

  再看:

1. int intArray[10];
2. cout << sizeof(intArray) ;


  第2行的输出结果为40(整型数组占据的内存空间大小)。

  如果C/C++程序可以这样写:

1. int[10] intArray;
2. cout << sizeof(intArray) ;

  我们就都明白了,intArray定义为int[10]这种数据结构的一个实例,可惜啊,C/C++目前并不支持这种定义方式。

  2、数组名可作为指针常量

  根据结论2,数组名可以转换为指向其指代实体的指针,所以程序1中的第5行数组名直接赋值给指针,程序2第7行直接将数组名作为指针形参都可成立。

  下面的程序成立吗?

1. int intArray[10];
2. intArray++


  读者可以编译之,发现编译出错。原因在于,虽然数组名可以转换为指向其指代实体的指针,但是它只能被看作一个指针常量,不能被修改。

  而指针,不管是指向结构体、数组还是基本数据类型的指针,都不包含原始数据结构的内涵,在WIN32平台下,sizeof操作的结果都是4。

   顺便纠正一下许多程序 员的另一个误解。许多程序员以为sizeof是一个函数,而实际上,它是一个操作符,不过其使用方式看起来的确太像一个函数了。语句 sizeof(int)就可以说明sizeof的确不是一个函数,因为函数接纳形参(一个变量),世界上没有一个C/C++函数接纳一个数据类型(如 int)为"形参"。

  3、数组名可能失去其数据结构内涵

  到这里似乎数组名魔幻问题已经宣告圆满解决,但是平静的湖面上却再次掀起波浪。请看下面一段程序:

1. #include <iostream.h>
2. void arrayTest(char str[])
3. {
4.   cout << sizeof(str) << endl;
5. }
6. int main(int argc, char* argv[])
7. {
8.   char str1[10] = "I Love U";
9.   arrayTest(str1);
10.  return 0;
11. }

  程序的输出结果为4。不可能吧?

  一个可怕的数字,前面已经提到其为指针的长度!

  结论1指出,数据名内涵为数组这种数据结构,在arrayTest函数体内,str是数组名,那为什么sizeof的结果却是指针的长度?这是因为:

  (1)数组名作为函数形参时,在函数体内,其失去了本身的内涵,仅仅只是一个指针;

  (2)很遗憾,在失去其内涵的同时,它还失去了其常量特性,可以作自增、自减等操作,可以被修改。

  所以,数据名作为函数形参时,其全面沦落为一个普通指针!它的贵族身份被剥夺,成了一个地地道道的只拥有4个字节的平民。

  以上就是结论4。

  结束语

  最后,笔者再次表达深深的希望,愿我和我的同道中人能够真正以谨慎的研究态度来认真思考开发中的问题,这样才能在我们中间产生大师级的程序员,顶级的开发书籍。每次拿着美国鬼子的开发书籍,我们不免发出这样的感慨:我们落后太远了。

 

 

转自:http://blog.chinaunix.net/u1/52321/showart_413155.html

转载于:https://www.cnblogs.com/dengqiang/archive/2009/11/03/1595328.html

Guess you like

Origin blog.csdn.net/weixin_34391445/article/details/93240782