C pointer parsing and pointer Parsing

Source: Internet
Author: User

C pointer parsing and pointer Parsing

This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020

A pointer is a special variable that represents an address, and the address can be subtracted from or added with an integer to represent a new address.

The following code analyzes the arithmetic operations of pointers:

# Include <stdio. h> int main () {int I; char str [] = {'A', 'B', 'C', 'D', 'E '}; int * p = str; char * q = str; q ++; p ++; printf ("str's first address: % x \ n", str ); printf ("internal zone pointed by pointer q: % x \ n", q); printf ("memory zone pointed by pointer p: % x \ n", p ); printf ("value of the first element of the array: % c \ n", * str); printf ("value of the first element of the array: % c \ n ", * p); // The first method to traverse the array. The subscript method printf ("the first method to traverse the array, the subscript method:"); for (I = 0; I <sizeof (str); I ++) printf ("% c", str [I]); // traverses the array. Method 2, array name pointer method printf ("\ n traversal array method 2, array name pointer method:"); for (I = 0; I <sizeof (str); I ++) printf ("% c", * (str + I); // traverses the third method of the array, the pointer method q = str; printf ("\ n traverses the third method of the array, pointer method: "); for (I = 0; I <sizeof (str); I ++) printf (" % c ", * q ++ );}
Execution result:


We can see that the first address of the array is 163c4730. through the pointer concept and four elements in the previous section, it is easy to know that the q type of the pointer is char *, and the q type of the pointer is char; char * q = str; the function is to assign the first address of the array str to the pointer q, that is, the value of the pointer q or the memory zone pointed by q is the first address of the array str. next, the pointer q is automatically added to 1. In this case, the compiler processes the pointer q with sizeof (char ), that is, a byte is moved backward based on the original address, that is, the memory zone pointed to by the pointer q is 163c4730 + 1 = 163c4731. similarly, the pointer p type is int * and the pointer type is int, int * p = str; the function is to assign the first address of the array to the pointer p, p ++, the compiler adds the sizeof (int) value of the pointer p, that is, four bytes are moved backward based on the original one, that is, the memory zone pointed by the pointer p is 163c4734, therefore, the pointer p points to the four bytes starting with unit 0th before the auto-increment, and points The four bytes starting from unit 4 of the array.

Therefore, when judging how many bits the pointer moves backward, You need to determine the type to which the Pointer Points.


You can also use pointers to traverse the array. For example, you can use the array name as the array name points to the cell 0th of the array. If an offset is added to the array, you can access other elements of the array. Therefore, you can use the array name + offset to traverse the array. Similarly, another pointer points to the cell 0th of the array. Each time the pointer is incremented by 1, the entire array can be traversed. Note that the type pointed to by the pointer, to be consistent with the element type of the array, the above Code uses the pointer q to traverse the array, instead of using the pointer p to traverse.

Of course, if the pointer is subtracted from an integer, the processing process is similar to the previous process, but only moves forward rather than backward.

To sum up, after a pointer ptrold is added with an integer n, the result is a new pointer ptrnew. The type of ptrnew is the same as that of ptrold, ptrnew points to the same type as ptrold. The value of ptrnew will be increased by n multiplied by sizeof (type pointed to by ptrold) bytes than the value of ptrold. That is to say, ptrnew will point to a memory area that moves n byte by sizeof (type pointed by ptrold) to the high address direction than the memory area pointed to by ptrold. After a pointer ptrold is subtracted from an integer n, the result is a new pointer ptrnew. The type of ptrnew is the same as that of ptrold, and the type of ptrnew is the same as that of ptrold. The value of ptrnew will be less than the value of ptrold by n multiplied by sizeof (type pointed to by ptrold) bytes, that is, the memory area to which ptrnew points will move n times of sizeof (type pointed by ptrold) bytes to the lower address direction than the memory area to which ptrold points.





Detailed explanation of pointer (*) in C Language (to be explained)

In fact, there are only variables and constants in the C language, and there are no pointers at all. Pointers are also variables. A variable is a nameized bucket. A variable has a dual attribute. One is a value, and the other is an address (physical address, that is, the location in the memory ). The variable name and value are bound together. With the variable name, we can access the values stored in the bucket it represents, you can put the new value in the bucket it represents (and the original value is overwritten ). Another attribute of the variable, the address, is implicit. In C language, we need to use the get address operator before the variable name to get it. For example:
Int a, B; declares a variable. we can write a = 3 (put 3 in the bucket represented by a), and we can write B = a + 2; we take the value in a and add the value 5 after adding 2 to the bucket represented by B. If we want to know where the storage space represented by a is in the memory, we can write & a. At this time & a sets the location of the memory space represented by, the address of variable a is retrieved. Because the address of each unit in the memory is marked with a positive integer (like the seat code 1, 2, 3... so the address can also be treated as a value (isn't a positive integer a value ?) Let's take a look at it. Suppose & a = 1024. Since the address is a value, we can store it in another bucket. That's exactly what we mean by writing int * p = &.

You may wish to remember every sentence I have said to you here, especially the first two sentences. It doesn't matter if you don't understand it now. When it comes, you will naturally understand it, then you will know that this is Jin Yu liangyan!

C language pointer

B.

Feof is used to check whether the file pointer opened by the current fp is at the end of the file. At the end, no value is returned.

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.