#include <stdio.h>
void main()
{
int a,b,sum;
printf("ab");
scanf("%d,%d",&a,&b);
sum=a+b;
printf("sum=%d\n",sum);
}
#include <stdio.h>
void main()
{
int a,b,sum=0;
int *p_1,*p_2;
printf("ab");
scanf("%d,%d",&a,&b);
p_1=&a;p_2=&b;
sum=*p_1+*p_2;
printf("sum=%d\n",sum);
}
56
a
1000
1000
b
,*”
*
&——……
*——……
int a = 10;
int *p;
p = &a; a p10
10 100
int a=10,int *p
p=&a
100 104
100 104
2-1
&
ptr_var = &var;
ptr_var2 = ptr_var;
2-2
*ptr_var = 10;
ptr_varvar10var
int a=5;/*a3000*/
int *p;
p=&a;
(1):p; (2)&p (3)*p
(4):a (5)&a
int num1=2,num2,*pnt;
pnt=&num1;
num2=*pnt;
(1)num1 (2) num2 (3)*pnt
#include <stdio.h>
void main()
{
int a,b;
int *p_1,*p_2,*max;
p_1=&a;p_2=&b;
printf("ab");
scanf("%d,%d",&a,&b);
if (a<b)
{
max=p_1;p_1=p_2;p_2=max;
}
printf("%d,%d\n",*p_1,*p_2);
}
3-1
&”
&ary[2]
+
ary + 2
3-2
#include <stdio.h>
void main()
{
static int ary[10] = {1,2,3,4,5,6,7,8,9,10};
int i;
for (i = 0; i < 10; i ++)
{
printf(“\ni=%d,ary[i]=%d,*(ary+i)=%d",i,ary[i],*(ary + i));
printf("&ary[i]= %X,ary+i=%X",&ary[i],ary+i);
}
}
3-3
#include <stdio.h>
void main()
{
int a[5]={6,7,8,9,10};
int i;
for (i=0;i<5;i++)
{
printf("a[%d]:%d\n",i,a[i]);
}
}
#include <stdio.h>
void main()
{
int a[5]={6,7,8,9,10};
int *p;
for (p=a;p<a+5;p++)
{
printf("%d\n",*p);
}
}
int var,*ptr_var;
ptr_var = &var;
ptr_var ++;
var1000 2 ptr_var 1002
2-2
++ptr_varptr_var++ var
--ptr_varptr_var-- var
ptr_var + i var i
ptr_var - i var i
++(*ptr_var)(*ptr_var)++ var 1
*(ptr_var++) var
int x,*pb;
A)pb=&x B) pb=x C)*pb=&x D)*pb=*x
pq
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
p^ q^ 10 20 30 40 50 60 70 80
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
p^ q^
1.*p-*q
2.*++p,p__
3.--*q,q___
4.*--q,q___
5.++*p,p___
6.(*p)--,p___
7.*q--,q____*q
10 20 30 40 50 60 70 80
Ary[2]=ary[3];//
&ary[2]=&ary[3];//
2-1
#include <stdio.h>
void main()
{
char ary[10]="chinese";
char *p;
p=ary+2;
printf("%c",*p);
}
2-2
#include <stdio.h>
#include <string.h>
void main ()
{
char a,str[81],*ptr;
printf("\nEnter a sentence,");
gets(str);//
printf("\nEnter character to search for");
a = getche();//
ptr = strchr(str,a);
/* return pointer to char */
printf("\nString starts at address,%u",str);
printf("\nFirst occurrence of the character is at,%u",ptr);
printf( \nPosition of first occurrence is,%d,ptr-str);
}
Strchr()
2-1
malloc()
malloc()
2-2
#include <stdio.h>
#include <malloc.h>
void main() {
int *p,n,i,j,temp;
printf("\n Enter number of elements in the array,");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
for(i=0;i<n;++i) {
printf("\n Enter element no,%d,",i+1);
scanf("%d",p+i);
}
for(i=0;i<n-1;++i)
for(j=i+1;j<n;++j)
if(*(p+i)>*(p+j)) {
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
for(i=0;i<n;++i)
printf("%d\n",*(p+i));
}
free() 3-1
free()
void free(void *ptr);
ptr malloc()calloc()realloc()
free() 3-2
#include <stdio.h>
#include <stdlib.h>
int main() {
int number;
int *ptr;
int i;
printf("How many ints?");
scanf("%d",&number);
ptr = (int *) malloc
(number*sizeof(int));
if(ptr!=NULL)
{
for(i=0 ; i<number ; i++)
{
*(ptr+i) = i;
}
for(i=number ; i>0 ; i--)
{
printf("%d\n",*(ptr+(i-1)));
}
free(ptr);
return 0;
}
else
{
printf("\nMemory allocation
failed.\n");
return 1;
}
}
free() 3-3
calloc() 3-1
calloc()malloc()
calloc()
void *calloc( size_t num,size_t size );
calloc() 3-2
#include <stdio.h>
#include <stdlib.h>
int main() {
float *calloc1,*calloc2;
int i;
calloc1 = (float *) calloc(3,sizeof(float));
calloc2 = (float *)calloc(3,sizeof(float));
if(calloc1!=NULL && calloc2!=NULL) {
for(i=0 ; i<3 ; i++) {
printf("\ncalloc1[%d] holds %05.5f ",i,calloc1[i]);
printf("\ncalloc2[%d] holds %05.5f ",i,*(calloc2+i));
}
free(calloc1);
free(calloc2);
return 0;
}
else {
printf("Not enough memory \n");
return 1;
}
}
calloc() 3-3
realloc() 3-1
realloc()
void *realloc( void *ptr,size_t size );
realloc() 3-2
#include<stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
int i;
ptr = (int *)calloc(5,
sizeof(int *));
if(ptr!=NULL)
{
*ptr = 1;
*(ptr+1) = 2;
ptr[2] = 4;
ptr[3] = 8;
ptr[4] = 16;
ptr = (int *)realloc(ptr,
7*sizeof(int));
if(ptr!=NULL)
{
ptr[5] = 32;
ptr[6] = 64;
for(i=0;i<7;i++)
{
printf(“ptr[%d]:%d\n",i,
ptr[i]);
}
realloc(ptr,0); /* free(ptr);*/
return 0;
}
realloc() 3-3