C学习代码

  1 //------------------------------------1
  2 #include<stdio.h>
  3 void main()
  4 {
  5     int a,b,*p1,*p2,*p;
  6     printf("input a,b:\n");
  7     scanf("%d%d",&a,&b);
  8     p1=&a;//地址给他,并不给值
  9     p2=&b;
 10     if(a<b)//比较找出最大值
 11     {
 12         p=p1;
 13         p1=p2;
 14         p2=p;
 15     }
 16     printf("a=%d,b=%d\n",a,b);
 17     printf("order:\n");
 18     printf("Max=%d,Min=%d\n",*p1,*p2);
 19 }
 20 //-----------------------------------2
 21 #include<stdio.h>
 22 void main()
 23 {
 24     int a,*p;
 25     p=&a;
 26     *p=10;//同时修改a的值
 27     printf("*p=%d\n a=%d\n p=%x\n",*p,a,p);
 28 }
 29 //--------------------------3
 30 #include<stdio.h>
 31 void main()
 32 {
 33     int a,*pointer1;
 34     pointer1=&a;
 35     a=10;
 36     printf("&a=%x\n",pointer1);//&*pointer与&a相同,*&a与*pointer一样
 37     printf("&*pointer=%d\n",&*pointer1);
 38     printf("a=%d\n",a);
 39     printf("*&a=%d\n",*&a);
 40 }
 41 //---------------------------------------4
 42 #include<stdio.h>
 43 void main()
 44 {
 45     int a,b;
 46     int *pointer1,*pointer2;
 47     a=100;
 48     b=10;
 49     pointer1=&a;
 50     pointer2=&b;
 51     printf("a=%d,b=%d\n",*pointer1,*pointer2);
 52 }
 53 //------------------------------5
 54 //-----------------下标法
 55 #include<stdio.h>
 56 void main()
 57 {
 58     int a[10];
 59     int i;
 60     for(i=0;i<10;i++)
 61         scanf("%d",&a[i]);
 62     for(i=0;i<10;i++)
 63         printf("%d",a[i]);
 64     printf("\n");
 65 }
 66 //-----------------------通过数组名
 67 #include<stdio.h>
 68 void main()
 69 {
 70     int a[10];
 71     int i;
 72     for(i=0;i<10;i++)
 73         scanf("%d",&a[i]);
 74     for(i=0;i<10;i++)
 75         printf("%d",*(a+i));
 76     printf("\n");
 77 }
 78 //--------------------------用指针变量
 79 #include<stdio.h>
 80 void main()
 81 {
 82     int a[10];
 83     int i,*p;
 84     for(i=0;i<10;i++)
 85         scanf("%d",&a[i]);
 86     for(p=a;p<(a+10);p++)
 87         printf("%d",*p);
 88     printf("\n");
 89 }
 90 //---------------------------------6
 91 #include<stdio.h>
 92 void main()
 93 {
 94     int a[3][4];
 95     int i,j;
 96     int (*p)[4];
 97     printf("input array:\n");
 98     for(i=0;i<3;i++)
 99         for(j=0;j<4;j++)
100             scanf("%d",&a[i][j]);
101     printf("output array:\n");
102     for(p=a,i=0;i<3;i++,p++)//行指针
103     {
104         for(j=0;j<4;j++)
105             printf("%d",*(*p+j));//使用列指针访问二维数组元素
106         printf("\n");//
107     }
108     printf("\n");
109 }
110 //-----------------7
111 #include<stdio.h>
112 void main()
113 {
114     int a[3][4];
115     int i,j,max,(*p)[4];
116     printf("input three student's four score:\n");
117     for(i=0;i<3;i++)
118         for(j=0;j<4;j++)
119             scanf("%d",&a[i][j]);
120     p=a;
121     max=**p;
122     for(i=0;i<3;i++)
123         for(j=0;j<4;j++)
124             if(max<*(*(p+i)+j))
125                 max=*(*(p+i)+j);
126     printf("max=%d\n",max);
127 }
128 /* p[i] = &a[i][0];,其实p=&a; 就可以了
129 int a[3][4];
130     int i,j,max,*p[3];
131     printf("input three student's four score:\n");
132     for(i=0;i<3;i++)
133         for(j=0;j<4;j++)  {
134             scanf("%d",&a[i][j]);
135                         p[i] = &a[i][0];
136                 }
137       
138     for(i=0;i<3;i++)
139         for(j=0;j<4;j++)
140                     printf("p : %d\n",p[i][j]);
141 }
142 
143     int a[3][4];
144     int i,j,max,*p[3];
145     printf("input three student's four score:\n");
146     for(i=0;i<3;i++)
147         for(j=0;j<4;j++)  {
148             scanf("%d",&a[i][j]);
149                         p[i] = &a[i][0];
150                 }
151       
152     for(i=0;i<3;i++)
153         for(j=0;j<4;j++)
154                     printf("p : %d\n",p[i][j]);
155 }
156 input three student's four score:
157 40
158 40
159 40
160 40
161 50
162 50
163 50
164 50
165 60
166 60
167 60
168 60
169 p : 40
170 p : 40
171 p : 40
172 p : 40
173 p : 50
174 p : 50
175 p : 50
176 p : 50
177 p : 60
178 p : 60
179 p : 60
180 p : 60*/
181 //-----------------------------8
182 #include<stdio.h>//-------------1
183 void main()
184 {
185     char string[]="How are you";
186     printf("%a%s\n",string);
187 }
188 #include<stdio.h>//-----------------------2
189 void main()
190 {
191     char*string="How are you";
192     printf("%s\n",string);
193 }
194 #include<stdio.h>//-------------------------3
195 void main()
196 {
197     char string[]="How are you";
198     char *s;
199     s=string;
200     printf("%s\n",s);
201 }
202 //----------------------9
203 #include<stdio.h>
204 void main()
205 {
206     char str1[30]="Very Good!",str2[30];
207     char *p1,*p2;
208     p1=str1;
209     p2=str2;
210     printf("复制前str1数组存放的串为:%s\n",str1);
211     while (*p1!='\0')
212     {
213         *p2=*p1;
214         p1++;
215         p2++;
216     }
217     *p2='\0';
218     printf("复制后str2数组存放的串为:%s\n",str2);
219 }
220 //------------------------10
221 #include<stdio.h>
222 void main()
223 {
224     char *ps="This is a picture";//ps指的是首地址
225     int n=10;
226     ps=ps+n;
227     printf("%s\n",ps);
228 }

 

posted @ 2013-06-10 22:33  herizai  阅读(262)  评论(0编辑  收藏  举报