The Chapter_04 array and pointer are not the same

Source: Internet
Author: User
Tags array definition

  • Array is not a pointer
"Arrays and pointers are the same" is a very dangerous and incorrect statement. ANSI standard:
      *x;          y[];   
Array DefinitionNot the same External declaration of pointerFile 1:
 mango [];
File 2:
          * mango;
The error code above is as follows:
  • What is definition and what is Declaration
The "object" in C Language (different from the 'object' in C ++ or other object-oriented programming languages, the object here is just LinkerRelated "things", such as functions and variables) must There is only one definitionBut it can have multiple extern declarations. Definition: Creates an object, determines the object type, and allocates memory. It can only appear in one place. Statement: Describes an object, the name of the object created (defined) elsewhere, which can be used in subsequent code. It can appear multiple times. A declaration is a common declaration that describes the objects created elsewhere, and a definition is equivalent to a special declaration that allocates memory for the objects. The difference between "address y" and "address y content": In the value assignment statement "X = Y;", X is the left value and is AddressIndicates the place where the results are stored. It can be seen at compilation; Y is the right value, meaning Content of the address represented by Y. The standard requires that the value assignment operator use the modifiable left value as the operand on its left side. The address of each symbol is Known during compilation. For pointers RuntimeObtain the current value before you can unreference it. The following code:
         a[] = ;
The address of array a is 0x002afd8c, which also stores the character ""-- Direct reference. For pointers:
         * p = ;

The IP address of the pointer p is 0x003cf808. The content in the address is still a four-byte address 0x012d5858, which is the character -- Indirect reference. To sum up, when p is defined as a pointer but referenced as an array, p [I] produces the following effect: The compiler adds the offset (I * step) to the p address and obtains the content stored in the memory address.But the correct method should be: Obtain the content stored at the p address and add it as the base address (the number of bytes depends on the number of digits of the machine) and the offset to generate an address. Access this address to obtain the content.
  • Match declaration with definition
Pointer variables are always located at the same address (known during compilation), but their content can be different at any time (you can point to different variables, which can have different values ). In contrast, the address of the array cannot be changed, and its content can be different in different cases.
  • Other differences between arrays and pointers
Arrays and pointers can both be initialized with string constants in their definitions, but their The underlying mechanisms are different.When defining pointers, the compiler Only allocate the space of the pointer.Unless it is initialized with a String constant during definition. In ansi c, the string constant created by the initialization pointer is defined as read-only. If you try to modify the value of this string through a pointer Undefined behavior. The array initialized by the string can be modified.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.