c+ pointer arithmetic

Source: Internet
Author: User

Main () {

     charstr1[]={' A ', ' B ', ' C ', ' d '};     charstr2 = {' 1 ', ' e '},{' Q ', ' d '},{' g ', ' H '} };     CHAR*P_STR1 = "123456";    printf ("%c \ r \ n", str1);    printf ("%c \ r\n ", * (str1+1));    printf ("%c \ r \ n ", * (* (str2+1) +1));    printf ("%s \ r \ n ", p_str1+1);     printf ("%c \ r \ n", * (p_str1+1));    printf ("%c \ r \ n", p_str1);      return0; }  Results:b b d 2345622   How to understand str1[]   and *? [] and * Both have two meanings and functions, which are embodied in the definition and operation. [] At the time of definition, the current definition is an array, * at the time of definition, a pointer is currently defined. The str1 in str1[] represents the pointer to the first element of the array, and we don't have to look at the pointer so mysteriously. He has nothing to do with a general variable except that he has an address integer in it, and the pointer can be run in a special way.   If a pointer is P then p is just his representation. The memory location in the symbol table is 54321111, and the location of the memory has another address, *p is the value of the memory that the pointer points to. P+1, what's going on? If the value of P is 54321111, then p+1=54321112? No, the addition operation of the pointer is not a simple arithmetic operation. First, the pointer depends on the type of variable it points to, and if the type is x in memory placeholder 8, then p+1 should be p+8*1. We look at  char str2 ={{' 1 ', ' e '},{' Q ', ' d '},{' g ', ' H '}; First str2 is the pointer to the first element of the entire array, and str2+1 gets the pointer to the second array, * (str2+1) Gets the pointer inside theContent, what is in this, because this is a two-dimensional array so, the contents of this is a pointer, * (* (str2+1) +1) so that the D is taken.  str1  what does it actually do? At the time of definition, he was explaining that STR1 was a pointer to a two-element, and it actually completed the calculation (str1+2) when the operation was performed (of course, when we referred to the value operation). That is to say, the   pointer  = * (pointer +x)   These two are exactly equal operations.

c+ pointer Operation

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.