Learning point C language (21): Data Type-array and pointer

Source: Internet
Author: User
1. No need to get the address of the array, because the array name itself is an address
 
# Include <stdio. h> int main (void) {char c = 'a'; char CS [] = "ABC"; printf ("% C, % s \ n", C, CS);/* Get the character and character array content */printf ("% P, % P, % P \ n", & C, Cs, & CS ); /* get the address of the character and character array. CS is no different from & CS */getchar (); Return 0 ;}

  

2. The address of the array element is continuous:

 
# Include <stdio. h> int main (void) {char CS [] = "ABC"; printf ("% P \ n", Cs, & CS [0], & CS [1], & CS [2]); getchar (); Return 0 ;}

  

3. The address represented by the array name is the address of the first element:

 
# Include <stdio. h> int main (void) {char STR [] = "ABC"; char * P1 = STR; char * P2 = & STR [0]; printf ("% P, % P \ n ", P1, P2); getchar (); Return 0 ;}

  

4. Access array elements through pointers:

 
# Include <stdio. h> int main (void) {char STR [] = "ABC"; char * P = STR; printf ("% C \ n", * P ); printf ("% C \ n", * p + 1); printf ("% C \ n", * P + 2); printf ("\ n "); printf ("% C \ n", * P); printf ("% C \ n", * ++ P); printf ("% C \ n ", * ++ P); getchar (); Return 0 ;}

  

5. traverse the array by pointer:

 
# Include <stdio. h> int main (void) {char STR [] = "123456789"; char * P = STR; int I; for (I = 0; I
 
# Include <stdio. h> int main (void) {char STR [] = "123456789"; char * P = STR; while (* P! = '\ 0') {printf ("% C \ n", * P); P ++;} getchar (); Return 0 ;}

  
# Include <stdio. h> # include <string. h> int main (void) {char STR [] = "123456789"; char * P = STR; int I; for (I = 0; I
 
# Include <stdio. h> int main (void) {int Nums [] = {111,222,333,444}; int * P = Nums; int I; for (I = 0; I

6. Note: pointer + 1 is to move a position based on the element size.

 
# Include <stdio. h> int main (void) {int Nums [] ={ 111,222,333,444}; int * P = Nums; int I; printf ("% d \ n", * P ); printf ("% d \ n", * (p + 1); printf ("% d \ n", * p + 1);/* this is not the case; this indicates that after the value is set, + 1 */getchar (); Return 0 ;}

  

7. the pointer above is actually a pointer to an array element. How can we declare a real array pointer?

# Include <stdio. h> int main (void) {int Nums [4] = {111,222,333,444}; int (* P) [4] = & Nums; /* Note the parentheses */printf ("% d \ n", (* P) [1]). /* it is not convenient to use */printf ("% d \ n", (* P) [2]); getchar (); Return 0 ;}

  

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.