[Review C] pointer

Source: Internet
Author: User

Recently, I took the time to re-read "data structure and algorithm (C language version)", and I felt a lot unfamiliar with the C language. So I took the time to review the C language and record it.

(The original image is edited by omnigraffle pro)

//// Main. C // practicec // created by viktyz on 13-6-8. // copyright (c) 2013 viktyz. all rights reserved. // # include <stdio. h> void find1 (char array [], char search, char ** Pa) {int I; for (I = 0; * (array + I )! = 0; I ++) {If (* (array + I) = SEARCH) {* pA = array + I;/* breakpoint 2 */break; /* breakpoint 3 */} else if (* (array + I) = 0) {* pA = 0; break ;}} int main (INT argc, const char * argv []) {char STR [] = {"afsdfsdfdf \ 0"}; char a = 'D'; char * p = 0; find1 (STR,, & P);/* breakpoint 1 */If (0 = P)/* breakpoint 4 */{printf ("not found! \ N ");} else {printf (" found! \ N ") ;}return (0 );}

The above Code comes from the complete handling of the C pointer (full version: Revision of the supplemental version), with = Yao Yunfei revision = Ding Zhengyu.

It is mainly used to demonstrate pointer usage. in Mac OS 10.8 and xcode 4.6.2, the single-step debugging result is as follows:

Breakpoint 1:

(lldb) p *p(char) $0 = '\0'(lldb) p p(char *) $1 = 0x0000000000000000(lldb) p &p(char **) $2 = 0x00007fff5fbff880(lldb) p *str(char) $3 = 'a'(lldb) p str[0](char) $4 = 'a'(lldb) p &str[0](char *) $5 = 0x00007fff5fbff88c "afsdfsdfdf"(lldb) p str(char [12]) $6 = "afsdfsdfdf" {  (char) [0] = 'a'  (char) [1] = 'f'  (char) [2] = 's'  (char) [3] = 'd'  (char) [4] = 'f'  (char) [5] = 's'  (char) [6] = 'd'  (char) [7] = 'f'  (char) [8] = 'd'  (char) [9] = 'f'  (char) [10] = '\0'  (char) [11] = '\0'}(lldb) p &str(char (*)[12]) $7 = 0x00007fff5fbff88c(lldb) p str[3](char) $8 = 'd'(lldb) p &str[3](char *) $9 = 0x00007fff5fbff88f "dfsdfdf"

Breakpoint 2:

(lldb) p **pa(char) $10 = '\0'(lldb) p *pa(char *) $11 = 0x0000000000000000(lldb) p pa(char **) $12 = 0x00007fff5fbff880(lldb) p &pa(char ***) $13 = 0x00007fff5fbff848(lldb) p i(int) $14 = 3(lldb) p array(char *) $15 = 0x00007fff5fbff88c "afsdfsdfdf"(lldb) p *array(char) $16 = 'a'(lldb) p *(array + i)(char) $17 = 'd'(lldb) p (array + i)(char *) $18 = 0x00007fff5fbff88f "dfsdfdf"

Breakpoint 3:

(lldb) p **pa(char) $19 = 'd'(lldb) p *pa(char *) $20 = 0x00007fff5fbff88f "dfsdfdf"(lldb) p pa(char **) $21 = 0x00007fff5fbff880(lldb) p &pa(char ***) $22 = 0x00007fff5fbff848

Breakpoint 4:

(lldb) p *p(char) $23 = 'd'(lldb) p p(char *) $24 = 0x00007fff5fbff88f "dfsdfdf"(lldb) p &p(char **) $25 = 0x00007fff5fbff880

Analysis by memory distribution chartAs follows:


The whole process is actually to pass the address & P: 0x00007fff5fbff880 into the find1 function, by changing the value stored in this address & P: 0x00007fff5fbff880 (p) (another address ), the number of targets (* P) to be modified.

When passed in, the value of & P: 0x00007fff5fbff880 is 0x0000000000000000, and the number of objects stored in 0x0000000000000000 is '\ 0 ',

Run the * pA = array + I; command to change the value 0x0000000000000000 in & P: 0x00007fff5fbff880 to 0x00007fff5fbff88f.

In this process, the value stored in & P: 0x00007fff5fbff880 in the main () function is 0x00007fff5fbff88f, and the target number (* P) of 0x00007fff5fbff88f is 'D '.

This achieves the goal of modifying the number of targets.

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.