2019-03-29
1. 分配空间之后才有真正的地址
int a = 0;
printf("a:%p\n", &a); // a:0117FBE0
int* p = NULL;
printf("p1:%p\n", p); //p1: 00000000
p没有分配空间之前指向的是NULL,只有如下用malloc分配之后才有真正的空间
p = (int*)malloc(sizeof(int));
printf("p2:%p\n", p); // p2:01594A50
2.printf必须放在一个函数里面使用,所以这里用test包起来了
#ifdef __cplusplus
extern "C" {
#endif
void test() { // printf的使用必须放在一个函数内,不能作为局部变量使用
const int a = 99;
int* p = (int*)&a;
*p = 299;
printf("%d\n", a);
printf("%d\n", *p);
}
#ifdef __cplusplus
}
#endif
3. 路径前需要加const,否则编译不过,加上const后也能往里面写内容
FILE* fp = NULL;
const char* fout = "C:\\Users\\xxxx\\Desktop\\test.txt";
fp = fopen(fout, "wt");
fprintf(fp, "%d,%f\n", 99,88.0);// 往test.txt文件中以%d和%f的格式写入99和88.000000
fclose(fp);
fp = NULL;
注:
1. fprintf(fp, "%d,%d\n", 99,88.0); // 若把88.0以%d的形式写入txt文件,则实际写入的是99和0
2. fopen函数的使用过程中编译不过的问题,在代码第一行(所有#include文件之前)加 #define _CRT_SECURE_NO_WARNINGS
4.数组清零memset的两种方式
char buf[5];
printf("sizeof: %d\n", sizeof(buf)); //5
memset((void *)&buf[0], 0, sizeof(char)*5); // 第一种
memset(buf, 0, sizeof(buf));//第二种
for (int i = 0; i < 5; i++) {
printf("%d, ", buf[i]);//0,0,0,0,0,
}
网友评论