The difference between a reference and a pointer, the difference between an array and a pointer

Source: Internet
Author: User

One: The difference between a reference and a pointer

1: A reference is not an object, it is just another name for an existing object, it must be initialized, and it cannot change the object it binds to, and each time that reference is used it accesses the object that was originally bound.

2: The pointer itself is an object that can be changed without initialization to change the object pointed to by the pointer


Two: The difference between a pointer and an array

Pointer

Array

The address where the data is saved

Save data

Indirect access to data

Direct access

Typically used for dynamic data structures

Typically used to store elements with the same number of fixed data types

Related Operations malloc (), free (), etc.

Implicit allocation and deletion

The same often points to anonymous data

is the data name itself



Is it the same in the C language for the following two cases?

Char a[] = "ABCDEFG";---------------1

Char *p = "ABCDEFG";-----------------2

When talking about these differences, you should first talk about how the variables are stored in the computer. From the compilation principle we know that for all variables he will allude to a symbol table. To simplify, here's a simple, easy-to-understand symbol table:

a

0xffaa

p

0XFFCC

Table 1 An example of a simple symbolic representation

The table above represents a variable, and 0XFFAA is the storage address of the contents of variable A; P represents another variable, and 0XFFCC is the storage address of the contents of the variable p. For array-type variables and pointer-type variables, their address represents a different meaning.

For array A:

This 0XFFAA address is the first address where the array contents are stored. The reference steps for A[i] are as follows:

Step one, take out the value of I, he and 0xffaa added;

Step two, remove the contents as (0xffaa+i).

For pointer P:

This 0XFFCC address is not the content of the string, but an address, the address is the first address of the string, the p[i] or the pointer * (P+I) application steps are as follows:

Step one, remove the contents of the 0XFFCC address, for example, 0XFFDF;

Step two, take out the contents of address 0xffdf.


Another thing to be reminded of is:

Char a[] = "ABCDEFG";---------------array contents can be modified (character array)

Char *p = "ABCDEFG";-----------------content cannot be modified (string constants)

In ANSI C, the initialization pointer is the string constant that is created, is defined as read-only, and if an attempt is made to modify the value of the string by a pointer, the program appears as defined behavior.

For more details, refer to: http://wenku.baidu.com/view/678d1925a5e9856a561260b1.html


The difference between a reference and a pointer, the difference between an array and a pointer

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.