I also want to learn C language-Chapter 14th: pointer and array

Source: Internet
Author: User

Partners! Let's continue to work on the subsequent part of the C pointer. Today we are going to learn about pointers and arrays. Yesterday, we learned how to install linux and how to open, save, and exit the emacs editor, as well as the most basic debugging functions of gdb. Are you familiar with it? Some friends say it's hard. I think it's because you didn't give up seriously. Come on! Today, I have practiced the Quick Guide for emacs for a long time. You may also remember to practice it frequently! Write a program together! (Also, I would like to declare that this blog is only intended to enhance my concentration, and to provide some learning references for my friends who studied C language two days later, I am so ironic about some so-called "Daniel", for example, how can I get out of favor! You should turn off your browser early! You are only one person who has studied for several or decades before me. The real Daniel is working as hard as a cow, eating grass, and getting milk .) (The code I wrote from today to forever will not comply with the C Standard. Please correct it !)

Pointer and array
Since an array also occupies a storage unit, it also has its own memory address. With the memory address, our pointer variable can also point to an array or an array element! Since memory is occupied, there must be corresponding pointers!

Pointer variable pointing to an array element

Int a [5], * p;

P = & a [0];

1. in C language, the array name represents the first address of the array, which is the address of the first element. Because the above assignment is equivalent to: p =;

2. the pointer to an array element can also be defined with a value: int * p = & a [0]; or int * p =;

Reference array elements

1. int a [5], * p = & a [1] If * p = 10; then, it indicates assigning a value to the array element a [1] pointed to by p. Equivalent to a [1] = 10;

2. If p points to an array element, p + 1 points to the next element of the array element. If p = & a [0], p + 1 indicates the address of array element a [1.

Conclusion: No matter what type of pointer + n, it is equivalent to the integer p + sizeof (type) * n.

Reference

If the initial value of p is & a [0], p + I and a + I can both represent the address of element a [I]; * (p + I) and * (a + I) both represent the value of the array element a [I] pointed to by the pointer p + I or a + I.

To sum up, there are two methods to reference an array element: Subscript: a [I]; pointer: * (p + I ).

Example:

Void main () {int a [5] = {1, 2, 3, 4, 5}; int * p = a; for (int I = 0; I <5; I ++) {printf ("% p: % d", p + I, * (p + I ));}}
Output result:

0012FF34: 1 0 subscript 0012FF38: 2 address with 40012FF3C: 30012FF40: 40012FF44: 5
Can we change the array name? Let's try:

Void main () {int a [5] = {1, 2, 3, 4, 5}; int * p = a; for (int I = 0; I <5; I ++) {printf ("% p: % d", a + I, * (a + I ));}}
The output is:

0012FF34: 10012FF38: 20012FF3C: 30012FF40: 40012FF44: 5
We can see that it is all right. Similarly, we can write as follows:

Void main () {int a [5] = {1, 2, 3, 4, 5}; for (int * p = a; p <a + 5; p ++) {printf ("% p: % d", p, * p );}}
The output results are the same!

But here we should be careful that the following error is:

Do not use the array name as ++! The array name is a constant! Everyone must remember!

Selection: The subscript method is preferred, because the efficiency of the subscript method is not lower than that of the pointer method and the subscript method is more readable.

Pointer subtraction: See the following code:

What is the result of the subtraction of the two addresses in this program ?! Everyone may think it is 4. Hey hey! Actually not! Yes. Because the two addresses must be divided by sizeof (type) after subtraction!

Let's take a look at how to calculate the array length:

Have you seen it clearly ?! This can also be found! But remember. Here, we only need to understand the memory. Do not write code like this at ordinary times!

Let's take a look at our last program example today:

Alas! Why! Why is an error running! Depressed! What's going on ?! Oh! Let's take a closer look at it because the constant area does not have the write permission!

Is there any way for him to run it! Actually, it's okay! You only need to modify the 1 mark of the executable file to run normally! Let's take a look:

We can open the supported files in binary mode:

Change 40 of 000228 to C0 and save it! You can run the executable file again! After reading the changes:

The program output is:

SzMsg: HelLo, pszMsg: HelLo

SzMsg: 6, pszMsg: 4
Hey! Running successfully! What is the reason ?! This is because the 40 represents readable and not writable. C0 indicates readable and writable!
 

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.