argc,avgv用法

argc 表示有几个参数,因为至少要指定一个应用程序的名,所以argc>=1. argv表示参数是什么。

int main(int argc, char **argv) {
printf("hello world.\n");
printf("argc=%d\n", argc);
int i = 0;
for (i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}

判断命令行输入有没有某个字符串.

如果没有返回-1,如果有把字符串后面的数字转化为整型.

int ArgPos(char *str, int argc, char **argv) {
int a;
for (a = 1; a < argc; a++) {
if (!strcmp(str, argv[a])) {
if (a == argc - 1) {
printf("Argument missing for %s\n", str);
exit(1);
}
return a;
}
}
return -1;
} int main(int argc, char **argv) {
int i;
if ((i = ArgPos((char *)"-size", argc, argv)) > 0) {
printf("argv=%d", atoi(argv[i + 1]));
}
return 0;
}

alloc

malloc和alloc的作用类似,申请连续的内存空间.不同是calloc申请空间的内容被清空.malloc申请空间的内容是随机的.

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct student {
int id;
int score;
};
struct student* s1;
struct student* s2;
/*
* malloc和alloc的作用类似,申请连续的内存空间.不同是calloc申请空间的内容被清空.
* malloc申请空间的内容是随机的.
*/
int main(int argc, char **argv) {
s1 = (struct student*)calloc(MAX, sizeof(struct student));
printf("%d, %d\n", s1[0].id, s1[0].score); s2 = (struct student*)malloc(MAX * sizeof(struct student));
printf("%d, %d\n", s2[0].id, s2[0].score);
return 0;
}

fscanf

把文件中的内容读取到内存

#include <stdio.h>
#include <stdlib.h>
int main(){
char str1[10], str2[10], str3[10];
int year;
FILE * fp;
fp = fopen ("file.txt", "w+");
fputs("We are in 2012", fp);
rewind(fp);//重新指向流的开头
fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);//把数据从文件中读到内存
printf("Read String1 |%s|\n", str1 );
printf("Read String2 |%s|\n", str2 );
printf("Read String3 |%s|\n", str3 );
printf("Read Integer |%d|\n", year );
fclose(fp);
return(0);
}

posix_memalign

linux支持posix_memalign,windows不支持.posix_memalign申请空间考虑了内存对齐的问题.和malloc,calloc相比效率更高.第一个参数要转化成(void**),第2个参数必须是2^n,第3个参数必须是第2个参数的倍数.最终申请的空间数是第3个参数指定的,申请空间的类型是*buf.

#include <stdio.h>
#include <stdlib.h>
int main() {
float *buf;
int ret;
ret = posix_memalign((void**)&buf, 256, sizeof(float) * 256);
if (ret) {
printf ("error.");
return -1;
}
int i;
for (i = 0; i < 256; i++) {
buf[i] = i;
}
for (i = 0; i < 256; i++) {
printf("%f\n", buf[i]);
}
return 0;
}

最新文章

  1. [PHP]加密解密函数
  2. 苹果系统安装虚拟机 Mac如何安装虚拟机教程
  3. NodeJs连接Oracle数据库
  4. ProtoType(原型)-对象创建型模式
  5. android: SQLite删除数据
  6. saiku执行速度慢
  7. Yii框架,在页面输出执行sql语句,方便调试
  8. ZOJ 1078 Palindrom Numbers
  9. 受益匪浅的材料orz
  10. 设计模式的征途—3.工厂方法(Factory Method)模式
  11. SpringCloud学习笔记(5)——Config
  12. nodejs cluster模块初探
  13. Java中excel与对象的互相转换的通用工具类编写与使用(基于apache-poi-ooxml)
  14. css学习の第二弹—文字格式化排版
  15. CSV的简单用法
  16. mysql如何处理亿级数据,第一个阶段——优化SQL语句
  17. java 常用命令
  18. UVA11077 Find the Permutations
  19. Django商城项目笔记No.7用户部分-注册接口-判断用户名和手机号是否存在
  20. python 文件不存在时才能写入,读写模式xt

热门文章

  1. .NET后端知识汇总
  2. Python数据挖掘入门与实战PDF电子版加源码
  3. 获取单列集合,双列集合,数组的Stream流对象以及简单操作
  4. [FPGA]Verilog实现可自定义的倒计时器(24秒为例)
  5. 开源WPF控件库MaterialDesignInXAML推荐
  6. windows和linux的开机顺序
  7. https的安装(基于阿里云)
  8. Oracle SQL command slash
  9. 关于jsp中jstl报错Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core
  10. Java数组与C/C++数组的区别