7、字符串常量
#include
int main(int argc,int **argv){
printf ("%s","abcdefgh"+2);
}
dp@dp:~/test1 % cc test3.c -o mytest
dp@dp:~/test1 % ./mytest
cdefgh
8、函数指针
本博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
通过如下格式来声明函数指针:
返回类型 (*函数指针变量名)(参数列表)
int add(int a,int b);
int main(void){
int (*myfunc)(int a,int b);
myfunc=add;
int x=myfunc(12,36);
printf("%d",x);
return 1;
}
int add(int a,int b){
return a+b;
}
~
dp@dp:~/test1 % cc test1.c -o mytest
dp@dp:~/test1 % ./mytest
48
8、命令行参数
打印参数个数,注意,命令本身也是一个参数,所以argc至少为1。
#include
int main(int argc,char **argv){
printf("%d\n",argc);
return 1;
}
~
dp@dp:~/test1 % cc test2.c -o mytest
dp@dp:~/test1 % ./mytest 12
下面没有使用argc参数,直接使用了argv参数,通过判断是否null,来决定参数列表是否结束
#include
#include
int main(int argc,char **argv){
while (*++argv!=NULL)
printf("%d\n",argv);
return 1;
}
~
dp@dp:~/test1 % cc test2.c -o mytest
dp@dp:~/test1 % ./mytest -a
-a
dp@dp:~/test1 % ./mytest -a 12 24
-a
12
24
通过如下格式来声明函数指针数组:
返回类型 (*函数指针变量名[])(参数列表)
下面结合函数指针数组与命令行完成一些简单的运算,通过命令行传送运算符与数字。
#include
#include
int add(int a,int b){
return a+b;
}
int sub(int a,int b){
return a-b;
}
int main(int argc,char **argv){
int (*operate_func[])(int,int)={
add,sub};
int myresult=0;
int oper=atoi(*++argv);
printf ("%d\n",oper);
int mynum;
while (*++argv!=NULL)
{
mynum=atoi(*argv);
printf ("%d ",mynum);
myresult=operate_func[oper](myresult,mynum);
}
printf ("\n%d\n",myresult);
return 1;
}
dp@dp:~/test1 % cc test2.c -o mytest
dp@dp:~/test1 % ./mytest 0 1 13 52
0
1 13 52
66
dp@dp:~/test1 % ./mytest 1 1 13 52
1
1 13 52
-66
dp@dp:~/test1 %