指向整形数组的指针

逍遥云 posted @ 2010年12月10日 23:48 in c/c++ with tags c pointer to array 数组针指 , 2101 阅读
    int a[10];
    memset(a, 0, sizeof(a));
    int *pt = a;
    *pt = 2;
    pt[1] = 1;
    pt++;
    printf("*pt = %d, pt[-1] = %d, sizeof(a) = %d\n", *pt, pt[-1], sizeof(a));
    --pt;
    *(pt + 10) = 77;
    
    int (*p)[10] = &a;
    memset(*p, 0, sizeof(*p));
    (*p)[0] = 10;
    (*p)[1] = 9;
    printf("**p = %d, (*p)[1] = %d, sizeof(*p) = %d\n", **p, (*p)[1], sizeof(*p));
    p++; //跳过40个地址长度,即a[10]连续地址之后,
    printf("**p = %d\n", **p); //得到77这个值
    --p; //为不影响后面的结果,修正p的指向为a[0]的地址;
    
    int (**pa)[10] = &p;
    memset(**pa, 0, sizeof(**pa));
    (**pa)[0] = 50;
    (**pa)[1] = 30;
    printf("***pa = %d, (**pa)[1] = %d, sizeof(**pa) = %d,\n", ***pa, (**pa)[1], sizeof(**pa));

    int **pp = &p;
    memset(*pp, 0, sizeof(**pa));
    (*pp)[0] = 31;
    (*pp)[1] = 32;
    (*pp)++;
    printf("**pp = %d, (*pa)[-1] = %d\n", **pp, (*pp)[-1]);
    --(*pp);

    int ***ppp = &pp;
    memset(**ppp, 0, sizeof(**pa));
    ***ppp = 40;
    (**ppp)[1] = 99;
    (**ppp)++;
    printf("***ppp = %d, (**ppp)[-1] = %d\n", ***ppp, (**ppp)[-1]);
    --(**ppp);

    int ****pppp = &ppp;
    memset(***pppp, 0, sizeof(**pa));
    ****pppp = 50;
    (***pppp)[1] = 100;
    (***pppp)++;
    printf("****pppp = %d, (***pppp)[-1] = %d\n", ****pppp, (***pppp)[-1]);

 

主要还是在于如何理解解引用,++操作只能用于指针变量,而*p与**pa却明确指向一个数组,如果*p++或者**pa++的话,gcc编译器会出现:

error: lvalue required as increment operand

这样的错误,然而p与*pa指向一个数组的首地址,如果使用++或--的话,就会有跨度,即直接跨过数组地址长度而指向下一块,对的,有点块的感觉。

Avatar_small
civaget 说:
2023年12月11日 23:46

After trying 러시아마사지, I can't go back to regular massages. It's truly exceptional.

Avatar_small
civaget 说:
2023年12月12日 03:50

I'm thrilled with 백링크하이's results. My website ranks higher, attracting more organic traffic.

Avatar_small
civaget 说:
2023年12月12日 19:41

제주유흥: Where entertainment meets tranquility. It exceeded all my expectations.

Avatar_small
civaget 说:
2023年12月14日 23:59 I like the helpful information you provide in your articles. I will bookmark your weblog and check again here frequently. I am quite certain I will learn plenty of new stuff right here! Good luck for the next! 스포츠중계
Avatar_small
civaget 说:
2023年12月19日 01:35

I've discovered niche content on 누누티비 that caters to my specific interests.

Avatar_small
civaget 说:
2023年12月24日 21:26

These무료스포츠중계websites are a gem for international sports fans. No more geographical restrictions.

Avatar_small
civaget 说:
2023年12月24日 22:28

A visit to 오산휴게텔 is an investment in well-being. The deep tissue massage alleviated my tensions, and the sauna added to the rejuvenating experience.

Avatar_small
civaget 说:
2023年12月26日 19:46

The popularity scoring system on 티비위키 is a game-changer.

Avatar_small
civaget 说:
2023年12月26日 22:03

This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post. Also, I have shared your website in my social networks! 프리카지노

Avatar_small
civaget 说:
2023年12月27日 04:36

Self-published authors can use social media to share behind-the-scenes glimpses of their writing process. self publishing on amazon

Avatar_small
civaget 说:
2023年12月31日 13:50

I would like to convey my admiration for your generosity in support of men and women that have the need for help with this particular concern. Your special dedication to getting the message all over had been wonderfully productive and have all the time made professionals much like me to attain their dreams. Your own invaluable tutorial means a great deal to me and additionally to my office workers. Thank you; from everyone of us. Divine Revelations

Avatar_small
civaget 说:
2024年1月03日 19:15

Edge Therapy's 강남 토닥이 is a sanctuary for women to rejuvenate, unwind, and rediscover serenity.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter