What is a pointer? What is an array? Pointer and array relationships?

Source: Internet
Author: User

What is a pointer? What is an array? Pointer and array relationships?

Figuratively speaking, we can think of the computer's memory as a row of houses on a long street, where each room can accommodate data and be identified by a room number. And the value of each room number we can call the address. Perhaps this analogy has limitations, after all, real computer memory is composed of tens of thousands of bits.

⑴ initialization:

① is the simplest example of an int *p, which opens up four bytes of space in memory during initialization.

▇▇▇▇

② Another statement, now we have two variables that show the memory of the unknown content in the back.

char ch = ' a ';

char CP = &a;

▇→▇▓← (? )

(CP) (CH)

⑵ use of "*"

① defines the pointer variable is

② a section reference to a pointer variable is

★ NOTE 1: To implement the move of the address, it is p++, not *p++

★ NOTE 2:

int *p = NULL; Represents the creation of a pointer variable and initializes it.

*p = null;//Change the contents of the access to null.

★ Note 3: What is the difference between an array int a[4],&a and a? The &a is the same as the content of a, but the &a+1 adds 16 bytes, and A+1 adds a byte. The reason is that &a is the address of the entire array, and a is the address of the first element of the array.


The table below gives us a deep understanding of the difference between pointers and arrays

a[]={1,2,3,4} Char name[]= "abcdef" Char *name= "abcdef"
sizeof (a) =16 sizeof (Name[0]) =1 sizeof (&name) =4
sizeof (&A) =16 sizeof (&name) =4 sizeof (*name) =1
sizeof (A+0) =4
sizeof (*name) =1 sizeof (&NAME+1) =4
=4sizeof (*a) =4 sizeof (&NAME+1) =4 sizeof (name) =4
sizeof (A+1) =4 sizeof (NAME+1) =4 sizeof (NAME+1) =4
sizeof (&A) =4 sizeof (name) =7 strlen (name) =6
sizeof (&a[0]) =4 Strlen (&name) =6 Strlen (&name) =6
sizeof (&A[0]+1) =4 Strlen (&name+1) =12 strlen (&name+1) = random value


The difference between a pointer array and an array pointer

Array of pointers: arrays that store pointers, that is, array elements are pointers.

Array pointer: A pointer to an array.


An expression Type
Said Element representation
int a[4]
Array of integral type Integer array with 4 elements A[i]
int *a[4] Array pointers The element in a is an int type pointer *a[i] or * (A[i])
int (*a) [4] Array pointers Pointer to array a (*a) I


▲int *: Access four bytes at a time.

▲char *: One-time access to a byte.








What is a pointer? What is an array? Pointer and array relationships?

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.