C语言若干问题

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 * pp指向的内容是常量~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__ ) )

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值