1. sizeof() 和strlen()
输出:
注意在C中,字符'a'的类型四int,而在C++中字符的类型为char。
2. C语言中符号的重载
static:
在函数内部,表示该变量的值在各个调用间一直保持延续性。
在函数这一级,表示该函数只对本文可见。
extern:
用于函数定义,表示全局可见(属于冗余的)。
用于变量,表示它在其他地方定义。
3. 什么是定义,什么是声明?
定义:只能出现在一个地方,用于创建新的对象。它首先确定对象的类型,然后分配内存。例如:int my_array[100]
声明:可以出现多次,用于指代其他地方(如其他文件)定义的对象,它描述对象的类型。例如:extern int my_array[];
其实,只要记住下面的内容,就可以区分二者:
声明相当于普通的声明:它所说的并非自身,而是描述其他地方创建的对象。ertern对象声明告诉编译器对象的类型和名字,对象的内存分配则在别处进行。由于并未在声明中为数组分配内存,所以并不需要提供关于数组长度的信息。对于多位数组,需要提供最左边一维数组之外的其他维的长度(给编译器足够的信息产生代码)。
定义相当于特殊的声明:它为对象分配内存。
4. memcpy()和strcpy()
char * strcpy ( char * destination, const char * source );
Copy string
Copies the C string pointed by source into the array pointed by destination, including the terminating null character.
To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
void * memcpy ( void * destination, const void * source, size_t num );
Copy block of memory
Copies the values of num bytes from the location pointed by source directly to the memory block pointed by destination.
The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
The function does not check for any terminating null character in source - it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).