简体   繁体   English

计算机是否为数组的变量名分配内存?

[英]Does a computer allocate memory for the variable name of an array?

In book Head First C , it says that the computer does not allocate memory for variable name of an array. 在《 Head First C》一书中,它说计算机没有为数组的变量名分配内存。 I got confused! 我很困惑! Is this really true? 这是真的吗?

But if it has a memory, why can I not assign another array to this one? 但是,如果有内存,为什么不能为该数组分配另一个数组?

A computer program is an abstraction which describes the functionality you wish to perform. 计算机程序是一种抽象,它描述了您希望执行的功能。 A compiler takes your description and translates it into machine code for the computer to execute. 编译器将您的描述转换为机器代码以供计算机执行。

A metaphor: 隐喻:

I tell you "Walk two blocks and turn left walk one block". 我告诉你“走两个街区,然后左走一个街区”。 You can then get to the store. 然后,您可以去商店。 I could also have said "Walk till you see the blue building on the left, turn 270 degrees and walk till you see the store". 我还可以说:“步行直到看到左边的蓝色建筑物,然后旋转270度,然后步行直到看到商店”。 In both cases you will do the same thing but the instructions (program in this metaphor) are the totally different, have a different number of characters and verbs, etc. 在这两种情况下,您都将执行相同的操作,但是指令(此隐喻中的程序)完全不同,具有不同数量的字符和动词,等等。

A computer program in C is the same -- it does not matter what you name the variables -- the code that the computer will actually run will take up the same space if you call a variable "a" or if you call the variable "aVeryLongVariableName". C语言中的计算机程序是相同的-不管变量名是什么-如果您将变量称为“ a”或将变量称为“变量”,则计算机将实际运行的代码将占用相同的空间。 aVeryLongVariableName”。 The compiler will keep track of the names but the final output will be the same. 编译器将跟踪名称,但最终输出将相同。

The identifier you are using to name an array is a pointer which actually holds the base address(Address Of First Element) of that array . 您用来命名数组的标识符是一个指针,它实际上持有该数组的基地址(第一个元素的地址)。

So, you can assign this address to another pointer , but assigning it to another arrays base address is not legal 因此,您可以将此地址分配给另一个指针,但是将其分配给另一个数组的基地址是不合法的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM