首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

小程序 指针求解!解决方案

2012-04-27 
小程序 指针求解!C/C++ code#include stdio.hvoid main(){int *pNULL,*qNULL,*tNULLint i0p&iq

小程序 指针求解!

C/C++ code
#include "stdio.h"void main(){    int *p=NULL,*q=NULL,*t=NULL;    int i=0;    p=&i;    q=p;    for (i=0;i<10;i++)    {        p=&i;        p++;    }    for(i=0;i<10;i++)        printf("%d ",q);    printf("\n");    for(i=0;i<10;i++)        printf("%d ",*q);    printf("\n%d ",*(q+1));    printf("\n%d ",*(q+2));    printf("\n%d ",*(q+3));    printf("\n%d ",*(q+4));    printf("\n%d ",*(q+5));    printf("\n%d ",*(q+6));    //t=&(q[3]);//    printf("\n%d",(*t));}


输出结果:
1245040 1245040 1245040 1245040 1245040 1245040 1245040 1245040 1245040 1245040

0 1 2 3 4 5 6 7 8 9
0
1245040
1245044
1245120
4199273
1 Press any key to continue

我就不明白了 明明是相同的地址怎么输出的数就是不一样捏?而且 下面那些地址加的也好诡异!!!

[解决办法]
for (i=0;i<10;i++)
{
p=&i;
p++;
}

这里p改变了.而且变的乱七八糟

[解决办法]
C/C++ code
  p=&i;    q=p;    for (i=0;i<10;i++)    {        p=&i;//·····这里写的好难理解哦        p++;    }
[解决办法]
呵呵,你在初始化时,指针已经指向最后了。下面再操作这个指针,必须回到起始位置。。。例如刚开始p=q;
初始化完成后,再p=q;这样就回到起始位置了。。明白了吧?

热点排行