C Programming tips

Source: Internet
Author: User

I have been learning the C language for N years, but I found that there are always some small places I will forget... just remember it!

2012.12.26

# Include <stdio. h> void main (void) {char ptr1 [] = "pear"; // String Array char * prt2 = "orange"; // define a pointer, point to the string char * ptr3 [2] = {"apple", "banner"}; // define the pointer array, pointing to two different strings respectively, char * prt2 upgraded char (* ptr4) [3] = {"Watermelon"}; // defines a pointer pointing to a string with a length of 2 characters, it is a char * prt2 upgrade printf ("ptr1: % s \ n", ptr1); // display the ptr1 String Array printf ("ptr1: % c \ n ", ptr1 [2]); // display the third character printf ("ptr2: % s \ n", prt2) of the ptr1 string array ); // display the ptr2 string pointer to the string printf ("ptr2: % s \ n", prt2 + 2 );// Display (ptr2 string pointer + 2) to the string printf ("ptr2: % c \ n", * (prt2 + 2); // display (ptr2 string pointer + 2) printf ("ptr3 [0]: % s \ n", ptr3 [0]); // display a string as long as the first address is given in the Variable. printf ("ptr3 [0]: % d \ n", ptr3 [0]); // display the value of printf ("ptr4: % s \ n", ptr4); printf ("ptr4 [0]: % s \ n ", ptr4 [0]); // display the entire ptr4 string printf (" ptr4 [1]: % s \ n ", ptr4 [1]); // display the string printf after ptr4 starts from 1*3 ("ptr4 [2]: % s \ n", ptr4 [2]); // display the strings following ptr4 starting from 2*3 // The following lines. ptr1 [2] is a variable, so you can assign values. * (prt2 + 2) is part of a String constant. And cannot be assigned a value. Ptr1 [2] = 'D'; printf ("ptr1: % c \ n", ptr1 [2]); // * (prt2 + 2) = 'D '; // printf ("ptr2: % c \ n", * (prt2 + 2); // char * p is a pointer and no memory is allocated, the "abc123ABC" he points to is read-only and cannot be changed. You must assign a value to him below. // char p [] is an array with allocated memory, is to copy "abc123ABC" to the memory, the memory is read/write // You just need to remember, the pointer is not allocated memory, it points to the system read-only memory, therefore, the read-only // array allocates memory, that is, copying the value in the system's read-only memory to its memory. Therefore, you can use a pointer to read/write a character string, when displaying characters, the pointer must be used to point to the content. // ptr1 is a numerical name and a pointer to an array. ptr1 [0] is the content, & ptr1 [0] is a pointer to ptr1 [0 .}

PS:

Char (* ptr3) [2] = {"apple", "banner "};
Warning C4047: 'initializing': 'Char (*) [2] 'differs in levels of indirection from 'Char [6]'
Error C2078: too initialize initializers
Because char (* ptr3) [2] is essentially the same as char (* ptr3), it only uses [2] to control the length of each pointer string.

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.