1.注意大小写:像FILE不能写成file之类
2.数组和指针:
定义: char a[10];
Char *p;
然后赋值: P=a;
则p[3]和a[3]是同一引用
3.字符串:如果定义 char ch[5]=”HELLO”,则编译器会报错,因为”HELLO”这个字符串实际上占用了六个字节,最后一个是结束符。
4.malloc的用法:
(1)用malloc首先要注意加库文件:
#include<stdlib.h>
#include<malloc.h>
(2)使用malloc:
P=(char*)malloc(10); //分配10个字节,p指针指向首地址
使用结束后记得 Free(p);
5.
char *const p修饰指针为常量指针指向内容可以是变量~既 p++这样的操作不合法 *p='3' 合法
我的心得:const限制的是p,所以指针p是一个常量,char 限制的是*p,所以p指向的内容为字符。
const char * p是p指向的内容是常量~p是变量~
~既 p++合法 *p='3' 不合法
我的心得:char修饰的是*p,即指的是p指针指向的内容为字符,constant限制的也是*p,指的是*p是一个常量,即P指向的内容是一个常量。
指向函数的指针:
// type_names.cpp
// Prototype twofunctions.
void func1(){};
void func2(){};
// Define PVFN to represent a pointer to afunction that
// returns type void.
typedef void(*PVFN)();
int main()
{
// Declare an array of pointers tofunctions.
PVFN pvfn[] = { func1, func2 };
// Invoke one of the functions.
(*pvfn[1])();
}
Union 的用法:
Union内部可以有很多的成员变量,但是一个union变量在一个时刻只可以是其中的一个,而不能像结构体那样同时拥有多个属性。
例子:
union /* Defines a two-dimensional */ { /* array named screen */ struct { unsigned int icon : 8; unsigned color : 4; } window1; int screenval; } screen[25][80]; |
说明:The screen array contains 2,000 elements. Each element of the arrayis an individual union with two members: window1 and screenval. The window1 member is a structure with two bit-field members, icon and color. The screenval member is an int. At any given time, each union element holds either the int represented by screenval or the structure represented by window1.
在宏定义中“\”的意义:
Youcan also use the backslash (\) as a continuation character. When anewline character (equivalent to pressing the RETURN key) immediately followsthe backslash, the compiler ignores the backslash and the newline character andtreats the next line as part of the previous line. This is useful primarily forpreprocessor definitions longer than a single line. For example:
| Copy Code |
#define assert(exp) \ ( (exp) ? (void) 0:_assert( #exp, __FILE__, __LINE__ ) ) |