C: pointer and array (2) c: pointer and array (1)

Source: Internet
Author: User

Mind Map

This section describes the pointer and array (1) in the previous Article C. Pointers and arrays -- perform expansion above

Do you know what X = y is like during compilation and running?

Character pointer and function 1> A string is an array of characters ending with '\ 0. For example, printf accepts a pointer pointing to the first character in the character array.

This example and the following twoCodeIt is a truth.

2> write several common character functions.

1 >>> strcat (S, T) function, which copies the characters that t points to after the characters that s points? -- Note '\ 0'

# Include <stdio. h>
# Include <assert. h>

/* Strcat (Ps, T): copy the charactor pointed
* By t append to the character pointed by S
*/
Void * Strcat ( Char * Ps, Char * T ){
Char * ADDR = Ps;
Assert (PS! = NULL) & (T! = NULL ));
While (* PS ){ /* The s point to the last character */
PS ++;
}

While (* PS ++ = * t ++ ){ /* Copy t append to the S */
}

Return ADDR;
}

Int Main (){
Char S [5] = " AB " ;
Char * T = " E " ;
Printf ( " % S \ n " , Strcat (S, T ));
Return 0 ;
}

Note: In strcat, the two pointers PS and T. After the method ends, both of these functions point to the end of '\ 0', because the PS ++ operation is "get the result first, post auto-increment"
Memory changes:

Jin Yulan

2 >>> function strend (S, T): If String T appears at the end of string S, return 1; otherwise, return 0. Strend

 Int Strlen ( Char * P ){
Int Ilen = 0 ;
While (* P ){
Ilen ++;
P ++;
}

Return Ilen;
}

/* Strend: Find t in S */
Int Strend ( Char * S, Char * T ){
Int Slen = strlen (s );
Int Tlen = strlen (t );

If (Tlen> slen) Return 0 ;

While (* S)
S ++;

While (* T)
T ++;

For (; Tlen --;){
If (* -- T! = * -- S) Return 0 ;
}

Return 1 ;
}

Int Main (){
Char * S = " Hell wold, chuanshanjia " ;
Char * T = " Chuanshanjia " ;

Printf ( " % D \ n " , Strend (S, T ));
Return 0 ;
}
3 >>> strncpy (s, t, n) copies the first n characters in t to S. Strncpy (s, t, n)

 1 # Include <stdio. h>
2
3 Int Strlen ( Char * P ){
4 Int I = 0 ;
5 While (* P ){
6 P ++;
7 I ++;
8 }
9
10 Return I;
11 }
12
13 /* T contains a maximum of the first n characters to be copied to S. */
14 Char * Strncpy (Char * S, Char * T, Int N ){
15 Char * ADDR = s;
16 If (! Strlen (s) |! N) Return 0 ;
17
18 Int Tlen = strlen (t );
19 If (! Tlen) Return 0 ;
20
21 If (Tlen <n)
22 N = tlen;
23
24 // Move the pointer s to the last
25 While (* ++ S );
26
27 While (N --){
28 * S ++ = * t ++;
29 }
30
31 Return ADDR;
32
33 }
34
35
36
37 Int Main (){
38 Char S [ 20 ] = " Hell world " ;
39 Char * T = " Chuanshanjia " ;
40
41 Printf ( " % S \ n " , Strncpy (S, T, 2222 ));
42 Return 0 ;
43 }
Command Line Parameter 1. The first parameter (commonly used in argc) (number of command line parameters during running; the second parameter (commonly used in argv) is a pointer to a string array. 2. C language, argv [0], indicates startup Program . 3. Instance

Running result:

Memory Allocation:

 

Explanation:

> Argv is a pointer to the pointer array.

Therefore, the accepted parameters can also be written as follows:

In combination, we can understand that: From the inside out

 

Complex statement

1. Read -- rules from right to left.

> Rule symbol:

-----------------------------------------------------------

* Read as "pointer"

[] Reading as "an array"

() Read as "function of returning"

-----------------------------------------------------------

The following two examples:

Int * F (); // F: returns a pointer to the int type.

> Steps:

1) Find the identifier F: read as "F is ..."

2) look to the right and find that "()" reading "F is a function that returns"

3) You can see nothing to the right. You can look to the left and find *. "F is the function that returns the pointer"

4) continue to look at the left and read int as "F is a function that returns a pointer to the int type"

 

INT (* PF) (); // PF is a pointer to a function with the int type returned.

1) identifier PF, read as "PF is ..."

2) look to the right, find), look to the left, find *, read as "PF is a pointer"

3) look to the right and find "()". Read it as "PF is a pointer to the function that returns"

4) look to the right, No. Look to the left and find Int. Read it as "PF is the pointer to the function that returns the int type"

To sum up, I recently focused on C because I want to have a better understanding of PHP Kernel Language and server module development. The current C language gives me a better understanding of the computer.

 

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.