C指针原理 (26)-C指针基础
2013-12-05 22:54
309 查看
7、字符串常量
#include <stdio.h>
int main(int argc,int **argv){
printf ("%s","abcdefgh"+2);
}
dp@dp:~/test1 % cc test3.c -o mytest
dp@dp:~/test1 % ./mytest
cdefgh
8、函数指针
通过如下格式来声明函数指针:
返回类型 (*函数指针变量名)(参数列表)
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 <stdio.h>
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 <stdio.h>
#include <stdlib.h>
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 <stdio.h>
#include <stdlib.h>
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 %
#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
#include <stdlib.h>
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 <stdio.h>
#include <stdlib.h>
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 %
相关文章推荐
- C指针原理 (25)-C指针基础
- C指针原理(33)-C指针基础
- C指针原理(96)-C基础综合应用
- C指针原理(14)-C指针基础
- C++基础7【难】 多态:实现原理 vptr指针 证明vptr存在 类的步长 纯虚函数:抽象类 案例 【面试题】
- C指针原理(16)-C指针基础
- C指针原理(15)-C指针基础
- C指针原理(17)-C指针基础
- C指针原理(21)-C指针基础
- C指针原理(18)-C指针基础
- C指针原理(19)-C指针基础
- 指针06 - 零基础入门学习C语言46
- C++面试题(四)——智能指针的原理和实现
- 三大常用机密算法原理与基础使用MD5、3DES、RSA
- MySQL----基础架构及运行原理
- C基础--二重指针和数组指针
- JavaWeb开发之十:JSP运行原理与基础
- OpenLayers 3实践与原理探究2-ol3基础入门案例
- OpenLayers 3实践与原理探究4.1-ol3源码分析-底层基础
- [3D基础]切空间推导原理(3)