81. pointer with a pointer to a method for sorting a string 5 and outputs (pointer)
#include <stdio.h>
#include <string.h>
void sort(char **p,int n)
{
int i,j;
char *temp;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
if(strcmp(*(p+j),*(p+j+1))>0)
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
int main()
{
const int N=5;
int i;
char **p,*pstr[N],str[N][20];
for(i=0; i<N; i++)
pstr[i]=str[i];//将第i个字符串的首地址赋予指针数组pstr的第i个元素
for(i=0; i<N; i++)
scanf("%s",pstr[i]);
p=pstr;
printf("排序后:\n");
sort(p,N);
for(i=0; i<N; i++)
{
printf(pstr[i]);
printf("\n");
}
printf("\n");
return 0;
}
operation result:
82. A two-dimensional array according to any output line according to any one of element value (pointer)
#include <stdio.h>
int main()
{
int a[3][4]= {1,3,5,7,9,11,13,15,17,19,21,23};
int (*p)[4],i,j; //指针变量p指向包含4个整型元素的一维数组
p=a;
scanf("%d %d",&i,&j);
printf("a[%d][%d]=%d\n",i,j,*(*(p+i)+j)); //输出a[i][j]
return 0;
}
operation result:
83. The string copied as a string b, then the output string B (pointer)
#include <stdio.h>
int main()
{
char a[]="I am a student.",b[20];
int i;
for(i=0; *(a+i)!='\0'; i++) //逐个值进行复制
*(b+i)=*(a+i);
*(b+i)='\0';
printf("%s\n",b);
return 0;
}
#include <stdio.h>
void copy_string(char *from,char *to) //从from传给to
{
while((*to=*from)!='\0')
{
to++;
from++;
}
}
int main()
{
char a[]="I am a student.",b[20],*p1,*p2;
p1=a;
p2=b;
copy_string(p1,p2); //从p1传给p2
printf("%s\n",b);
return 0;
}
operation result:
84. The variable format output functions
#include <stdio.h>
int main()
{
int a;
float b;
scanf("%d %f",&a,&b);
char *format; //等同于char format[]="a=%d b=%.2f\n";
format="a=%d b=%.2f\n";
printf(format,a,b);
return 0;
}
operation result:
85. points to access functions through a pointer variable
#include <stdio.h>
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
int min(int a,int b)
{
if(a<b)
return a;
return b;
}
int main()
{
int (*p)(int,int); //p为一个指向函数的指针变量
int a=1,b=2,n,c;
scanf("%d",&n);
if(n==1)
p=max; //通过指针变量调用max函数
else if(n==2)
p=min;
c=(*p)(a,b); //p的作用是将函数max的入口地址赋给指针变量p
if(n==1)
printf("max=%d\n",c);
else if(n==2)
printf("min=%d\n",c);
return 0;
}
operation result:
86. The plurality of character string in alphabetical order (ascending) Output (Pointer Array)
#include <stdio.h>
#include <string.h>
void sort(char *name[],int n)
{
char *temp;
int i,j,k;
for(i=0; i<n-1; i++)
{
k=i;
for(j=i+1; j<n; j++)
if(strcmp(name[k],name[j])>0)
k=j;
if(k!=i) //不是移动字符串,而是改变指针数组的各元素的指向
{
temp=name[i];
name[i]=name[k];
name[k]=temp;
}
}
}
void print(char *name[],int n)
{
int i;
for(i=0; i<n; i++)
printf("%s\n",name[i]); //name[i]相当于一个一维数组的地址
}
int main()
{
char *name[]= {"Follow me","BASIC","Great Wall","FORTRAN","Computer Design"};
int n=5; //指针数组,每个元素存放一个地址,相当于一个指针变量
sort(name,n);
print(name,n);
return 0;
}
operation result:
87. The use of pointers to data pointer variable
#include <stdio.h>
int main()
{
char *name[]= {"Follow me","BASIC","Great Wall","FORTRAN","Computer Design"};
char **p; //p为指向指针的指针
int i;
for(i=0; i<5; i++)
{
p=name+i; //p先指向name[0]的地址,*p取出name[0]的值
printf("%s\n",*p);
}
return 0;
}
operation result:
88. The output value of each element integer array with a pointer to a pointer variable data
#include <stdio.h>
int main()
{
int a[5]= {1,3,5,7,9};
int *num[5]= {&a[0],&a[1],&a[2],&a[3],&a[4]};
int **p,i;
p=num; //p的值为&num[0]
for(i=0; i<5; i++)
{
printf("%d ",**p); //*p为&a[0],**p为a[0]
p++;
}
printf("\n");
return 0;
}
operation result:
89. The array of pointers to function as a main parameter
#include <stdio.h>
int main(int argc,char *argv[]) //argc代表个数
{
while(argc>1)
{
++argv;
printf("%s\n",*argv); //*argv[]相当于**argv,所以*argv相当于字符串首地址
--argc;
}
return 0;
}
int main(int argc,char *argv[])
// argc is the number of parameters in the command line; the argv array is a pointer to a string
For example: command line dd China Beijing
The argv [0] file1 \ 0
argv[1] China\0
argv[2] Beijing\0
The output is China
Beijing
Proceed as follows:
First, Windows + R to open the DOS, locate the file to run the project, open the \ bin \ Debug, exe files below
Enter Debug, enter in DOS: dd China Beijing (dd which is running the project file)
90. The array of characters as a two parameter main function
#include <stdio.h>
int main(int argc,char *argv[]) //argc代表个数
{
int sum=0;
while(--argc>0)
sum+=*argv[argc]-'0';
printf("%d\n",sum);
return 0;
}
DOS Input: dd 12 34 56 78
Operation result output: 16 (1 + 3 + 5 + 7 = 16)
among them:
argv [0] is 12; * argv [0] is 1
argv [1] is 34; * argv [1] 3
argv [2] is 56; * argv [2] 5
argv [3] to 78; * argv [3] is 7