C和指针(Chapter3 &Chapter4)

来源:互联网 发布:苹果mac是什么意思 编辑:程序博客网 时间:2024/06/05 18:27
主要整理一些自己看c和指针(英文版)遇到的我自己认为不熟悉或者是重要的知识点。有一些词会用英文表述。

第三章主要讲了Data。The chapter describes data and three properties of variables----scope,linkage,storage class(作用域,链接属性,存储空间) are also described.

四种基本类型:integers, floating-point values, pointers, and aggregate types (聚合类型比如数组和结构体)
1.int
整型可以用八进制,十六进制表示比如017, 0x7b
字符类型char以单引号围起来(enclosed in apostrophies)比如'M', '\n', '??(', '\377'
而宽字符版本需要在前面加上L(wide character literals are written as an L followed by a character literal)比如 L'X', L'e'
枚举类型(enumerated type)本质也是整型
enum Color {RED, YELLOW, BULE};枚举类型默认是从0开始且依次递增,所以RED=0, YELLOW=1, BULE=2, 当然也可以自己赋值比如enum Color {RED=3, YELLOW, BULE=6};此时RED=3, YELLOW=4, BULE=6

2.floating/double
浮点型表示:3.1415   1E10   25.   6.02e23  .5等其中e表示指数比如1E10就是1 * 10 ^ 10(1乘以10的10次方)另外浮点型字面值默认是double除非加了后缀(followed by suffix)比如加上后缀l或者L表示long double 以及F或者f表示float

3.pointers
指针会和const关键词结合
int a = 5;
const int *p1 = &a;//int const *p1 = &a;
int *const p2 = &a;
p1 is a pointer to constant integer while p2 is a constant pointer to an integer;
p1是指向常量的指针(常量指针),不可以通过*p1来修改a的值但是p1可以修改
    int a = 1 , b = 2;
    int const *p1 = &a;
    //const int *p1 = &a;
    //*p1 = 3不合法;
    //p1 = &b合法;
    int * const p2 = &b;
    //p2 = NULL不合法;
    //*p2 = 3合法;
    const int * const p3 = &a;
    //p3 = NULL不合法;
    //*p3 = 3不合法;
指针还可以指向字符串常量,那是因为当一个字符串常量出现在表达式中(a string literal appears in an expression),它所使用的值是字符串所存储的地址。比如
#include <stdio.h>
int main()
{
    char *p ="hello";
    printf("%s\n", p);
    printf("%s\n", "hello");

    printf("%p\n", p);
    printf("%p\n", "hello");
    return 0;     
}
最后结果为
hello
hello
0000000000404000
0000000000404000


4.数组(Array)
the subscripts(下标) of arrays always begin at 0 and extend throuth  one less than the declared size of the array.
C编译器并不检查下标是否在合适的范围内

5.说明符(Specifier)
the specifiers may include keywords to modify the length and signedness of the identifier(标识符)包括short  long  signed  unsigned
声明的时候,int有时候可以省略当有上面这四个关键词其中一个的时候比如unsigned short int a即等于unsigned short a
下图中在同一个单元格里面即表示声明相同

The signed keyword are usually used only for chars because the other integer types are signed by default. It is implementation dependent whether char is signed or unsigned , so char is equivalent to either signed or unsigned  char 


6.Typedef
typedef char *ptr;
这样定义后,ptr a就表示char *a;
注意尽量用typedef而非#define来创造新类型名原因是可能会遇到以下情况
#define ptr char *
ptr a, b;
因为#define只是简单的字符替换所以就变成了char *a, b;此时a为char*,b为char

7.extern和static属性会在下篇文章中写,此处省略

8.for和while的一些异同
for statement in C is really just a shorthand notation for a while loop.
the difference between for and while loops is with continue;
in the for statement, a continue skips the rest of the body of the loop and then goes to the adjustment but in the while loop ,the adjustment is part of the body, so a continue skips it ,too.

9.switch与break
设计一个小程序统计输入的字符数,单词数以及行数,不考虑单词是否有效即遇到空格换行就算作一个单词且每个单词后只有一个空格

在这里就是充分利用了switch里面的特性,如果没有break,会把所有case标签语句都走一遍,因此我们正常写程序最好加上break除非是为了实现多个标签case执行同样的操作。同时最好加上default以增强程序的维护性以及健壮性。

10.goto语句
首先能尽量少用goto语句就少用,除非遇到类似嵌套循环如下图所示


0 0
原创粉丝点击