1. 编程时偶然发现,可以编写如下程序,用来查看操作系统是多少位
int main(int argc, char *argv[])
{
printf("%d\n", sizeof(int *));
return 0;
}
编译执行后,打印8说明系统是64bit, 打印4说明系统是32bit
2. OS X系统打开终端的命令:
command + t : 在同一窗口打开终端
command + n : 在新窗口打开终端
3. OS X 浏览器切换标签命令:
control + tab
4. C语言:变量的名称和在该变量后调用的函数名称不能一样,否则编译器(gcc)会报错。如下:
int test(int test)
{
test += 1;
printf("###test = %d\n", test);
return 0;
}
int main()
{
int test = 5;
test(test);
printf("$$$test is %d\n", test);
return 0;
}
编译时报错:error: called object ‘test’ is not a function
5. 当判断函数void *fun()的返回值是否为null时,可用如下语句:
if (NULL == fun())
或:
if (0 == fun())
6. 字符型数组赋值问题:
#include <stdio.h>
#include <string.h>
int str_printf(char *str)
{
char str_buf[100];
str_buf = str;
printf("###str_buf is: %s\n", str_buf);
return 0;
}
int main(int argc, char *argv[])
{
char input_str[100];
gets(input_str);
str_printf("input_str");
return 0;
}
编译时报错,编译环境: OS X, gcc
error:array type 'char [100]' is not assignable
分析: str_buf为数组str_buf[100]的首地址, 在语句 char str_buf[100]; 后, str_buf[100]的内存空间已经分配好, str_buf为已分配好的内存空间的首地址, 在编译 str_buf = str; 时就会报错
7. 关于gets()和scanf(), 建议用scanf()替代gets(), 因为使用gets()的程序, 编译万运行时会生成警告, 如下 (编译运行环境: OS X, gcc)
warning: this program uses gets(), which is unsafe.
8. 阅读下面的代码, 体会三个知识点:
1) 风格良好的程序在对指针解引用前对它进行检查
2)建议用scanf()替代gets()
3)函数的形参为指针, 函数体内对形参本身做操作, 对形参做间解引用操作. 调用完改函数后, 传入该函数的实参指针不会发生变化, 但该实参指针所指向的内存里的值会发生变化.
下面程序的功能为: 输入任意字符串, 而输出字符串只保留字母和数字
1 #include <stdio.h>
2 #include <string.h>
3
4 int str_only_alphabet_num (char *input_str, char *output_str)
5 {
6 int i = 0;
7 if (NULL == input_str) {//风格良好的程序在对指针进行解引用前, 要对指针做是否为NULL的判断
8 printf("###input_str is NULL\n");
9 return -1;
10 }
11 while (*input_str) {
12 if (NULL == input_str || NULL == output_str) {//风格良好的程序在对指针进行解引用前, 要对指针做是否为NULL的判断
13 printf("###input_str or output_str is NULL\n");
14 return -2;
15 }
16 if ((*input_str >= 'a' && *input_str <= 'z') ||
17 (*input_str >= 'A' && *input_str <= 'Z') ||
18 (*input_str >= '0' && *input_str <= '9')) {
19 *output_str = *input_str;
20 output_str++;
21 input_str++;
22 }
23 else {
24 input_str++;
25 }
26 }
27 *output_str = '\0';
28 printf("###output_str is: %s\n", output_str);
29 return 0;
30
31 }
32
33 int main(int argc, char *argv[])
34 {
35 char input_str[100];
36 char output_str[100];
37 printf("###output_str is: %x\n", (int)output_str);//与printf("######output_str is: %x\n", (int)output_str);打印结果一样
38 printf("input string.\n");
39 //gets(input_str);
40 scanf("%s", input_str);
41 str_only_alphabet_num(input_str, output_str);
42 printf("######output_str is: %x\n", (int)output_str);
43 printf("###after calling str_only_alphabet_num(), output_str is: %s\n", output_str);//output_str所指向的内存的值已经发生变化
44 return 0;
45 }
9. 下面代码的功能: 将两个字符串合并后输出, 比如str1为"hello", 字符串str2为"world",合并后输出的字符串为"helloworld".
代码实现时一定要注意, 将字符串str2合并到字符串str1后时,一定要确保存储str1的内存有足够的空间来继续存储str2
代码实现如下:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "hello";//str1没有分配足够的空间
char str2[] = "world";
char *p = str1;
char *q = str2;
while (*p) {
p++;
}
while (*q) {
*p = *q; //因为str1没有分配足够的空间,这条语句会改变str1所在内存后的内存里的值, 这样操作是存在潜在危险性的
p++;
q++;
}
*p = '\0';
printf("###p is: %s\n", p);
printf("###q is: %s\n", q);
printf("###str1 is: %s\n", str1);
printf("###str2 is: %s\n", str2);
return 0;
}
另一个问题: 字符串的结尾处一定要赋值为'\0'. 上面的代码, 注释掉语句*p = '\0'; 编译运行, printf("###str1 is: %s\n", str1);的结果为###str1 is: helloworldd
结果分析:str1和str2分配的内存空间是相邻的, 第二个while循环体循环结束后, 内存里的值如下表第二行所示, 这就解释了为什么在没有*p = '\0'语句的情况下, printf("###str1 is: %s\n", str1);的打印结果为###str1 is helloworldd
h | e | l | l | o | '\0' | w | o | r | l | d | '\0' |
h | e | l | l | o | w | o | r | l | d | d | '\0' |
对上面的代码优化更改如下:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "hello";
char str2[20] = "world";
char *p = str1;
char *q = str2;
//printf("###sizeof(str1) is: %d\n", sizeof(str1));
//printf("###strlen(str1) is: %d\n)",strlen(str1));
//printf("###sizeof(p) is: %d\n", sizeof(p));
//printf("###strlen(p) is: %d\n", strlen(p));
if (sizeof(str1) < (strlen(str1) + strlen(str2))) {
printf("###str1 have not enough space\n");
return -1;
}
while (*p) {
p++;
}
while (*q) {
*p = *q;
p++;
q++;
}
*p = '\0';
printf("###p is: %s\n", p);
printf("###q is: %s\n", q);
printf("###str1 is: %s\n", str1);
printf("###str2 is: %s\n", str2);
return 0;
}