Pointers to C language pointers arrays and struct bodies

Source: Internet
Author: User

A pointer to an array that initializes an array, using the traditional way of traversing

1 voidMain ()2 {3     inta[5] = {1,2,3,4,5 };4      for(inti =0; I <5; i++)5     {6printf"%d,%x\n", A[i], &a[i]);7printf"%d,%x\n", * (a+i), a+i);//equivalent a[i], &a[i]8     }9}

A is the first address of the array A, that is, the address of element 1, a + 1 for address +1, that is, to increase the length of int 4 bytes, point to the address of the next address is element 2

1 int a[51,2,3,4,5  }; 2 int // Create a null pointer 3 p = A; // p is a pointer variable and a is the address    of the array's first ADDRESS element 1

Because we use the pointer traversal to achieve the same effect.

1 // using the pointer loop 2  for (int *pp = A; pp<a+5; pp++)  // address + + 3 {4     printf ("%d,%x\n", *pp, pp); 5 }

With the above understanding, the pointer structure is actually the same.

1 //pointer structure Body2 structInfo3 {4     Charname[ -];5     intID;6 };7 8 voidMain ()9 {Ten     structInfo myinfo[5] = { {"ZC1",1},{"ZC2",2}, {"ZC3",3}, {"ZC3",3}, {"ZC4",4} }; One      A     //Normal Mode output -      for(inti =0; I <5; i++) -     { theprintf"%s,%d\n", Myinfo[i].name, myinfo[i].id); -         //Pointers -printf"%s,%d\n", (* (Myinfo+i)). Name, (* (MyInfo +i)). ID); -     } +  -     //using the pointer loop +     structInfo *px =MyInfo; A      for(; px < MyInfo; px++) at     { -printf"%s,%d\n", Px->name, Px->id);//pointer structure output mode pointer variable-Properties -printf"%s,%d\n", (*PX). Name, (*PX). ID);//pointer structure output mode * pointer variable. Properties -         //px->id equivalent (*PX). ID -     } -  in GetChar (); -  to}

    

  

Pointers to C language pointers arrays and struct bodies

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.