zoukankan      html  css  js  c++  java
  • C语言深度解剖

    一. 关键字

    1. long在32位机器上占用4个字节。

    2. 寄存器变量不能超过1个整型变量的长度且不能用&取地址

    3. 变量命名规则:bit btVariable, boolean bVariable, char cVariable, void *vpVariable, struct A stVariable, void(* fpGetModeFuncList_a[])(void), typedef struct SM_EventOpt{...}SM_EventOpt_st, *SM_EventOpt_pst; DataGotFromSD; 函数命名规则:GetDataFromSD()

    4. 定义变量时一定要初始化,因为编译器并不一定清空了这块内存的值,可能是无效值。

    5. sizeof:int a[100], sizeof(a) = 4*100 = 400byte; int *p = NULL, sizeof(*p) = 4byte;

    6. case后面跟的只能是整型或字符型的常量或常量表达式。

    7. float *p1; int *p2; p1=(float *)p2;    void *p1; int *p2; p1=p2(任何指针类型可以直接转换为void*类型)  如果函数参数可以是任何类型的指针,那么应该声明为void*;例如:void *memcpy(void *dst, const void *src, size_t len(如100*sizeof(int)))    void *memset(void *buffer, int c, size_t num)  int dst[100],src[100]; memset(dst,0,100*sizeof(int)); memcpy(dst, src, 100*sizeof(int))

    8. return不可返回指向栈内存的指针,因为该内存在函数体结束时被自动销毁了。char *test(){char a[30]; ... ; return a;}错误

    9. volatile:表示修饰的变量可能被修改,保证每次取值都从原变量地址处取。普遍运用于寄存器变量、多线程共享变量、端口数据。

    10. 大端模式:低字节存放在高地址中,高字节存放在低地址中。小端模式:相反。

    四. 指针和数组

    1. 数组名a无法作左值,当a作为右值时代表数组首元素的首地址。&a才是取整个数组的首地址。

    2. 指针数组:int *p[10];  数组指针:int (*p)[10]

    3. 指针变量(地址)与整数的加减:int p[10]; &p+1= &p+1*sizeof(10*sizeof(int))(因为&p是指整个数组的首地址,所以+1代表加了一个数组长度); p+1=p+1*sizeof(int) (因为p指的是数组首元素的首地址,所以+1只是增加了一个元素的长度);

    4. C语言中,当以一维数组作为形参时,编译器总是将它解析成指向该数组首元素首地址的指针

    5. 无法把指针变量本身传递给一个函数,可以通过return或者二级指针的方法解决。

    6. char *fun3(char *p1,char* p2):fun3是函数名;char* *fun2(char* p1,char* p2):fun2是二级指针;char*(*fun1)(char* p1, char* p2) = char*(*)(char* p1, char* p2) fun1:fun1是函数指针变量

    函数指针使用举例:char *fun(char* p1, char* p2){}; int main(void){ char *(*pf)(char* p1, char* p2); p1=&fun; (*pf)("aa", "bb");}

    7. 函数指针数组:char *(*pf[3])(char *p1)

    8. 函数指针数组的指针:char *(*(*pf[3]))(char *p1))

    五. 内存管理

    1. 静态存储区保存全局变量和静态static变量, 局部变量保存在栈, new和malloc变量保存在堆

    2. 不管什么时候使用指针之前都要确保指针是有效的。assert( NULL != p)

    3. 指针分配完内存后需要初始化:char *p = NULL; 数组初始化:int a[10] = {0} 或 memset(a,0,10);

    4. 内存释放:连续释放free两次肯定会发生错误。

    5. 内存释放后需要把指针变量设为NULL:p = NULL

    六. 函数

    1. 函数注释

    /************************************************************************ * Function Name : nucFindThread * Create Date : 2000/01/07 * Author/Corporation : your name/your company name * * Description : Find a proper thread in thread array. * If it’s a new then search an empty. * * Param : ThreadNo: someParam description * ThreadStatus: someParam description * * Return Code : Return Code description,eg: ERROR_Fail: not find a thread ERROR_SUCCEED: found * * Global Variable : DISP_wuiSegmentAppID * File Static Variable : naucThreadNo * Function Static Variable : None * *-----------------------------------------------------------------------* Revision History * No. Date Revised by Item Description * V0.5 2008/01/07 your name … … ************************************************************************/ static unsigned char nucFindThread(unsigned char ThreadNo,unsigned char ThreadStatus) { … }

    2. 递归函数:不用任何库函数、变量编写strlen函数。

    int my_strlen(char *strDest){ assert( NULL != strDest); if( '' == *strDest){ return 0;} else { return (1+my_strlen(strDest));}}

    七. 文件结构

    1. 头文件书写顺序:

    No. Item 1 Header File Header Section 2 Multi-Include-Prevent Section 3 Debug Switch Section 4 Include File Section 5 Macro Define Section 6 Structure Define Section 7 PrototypeDeclareSection

    如:

    /************************************************************************ * File Name : FN_FileName.h * Copyright : 2003-2008 XXXXCorporation,All Rights Reserved. * Module Name : DrawEngine/Display * * CPU : ARM7 * RTOS : Tron * * Create Date : 2008/10/01 * Author/Corporation : WhoAmI/yourcompany name * * AbstractDescription : Place some descriptionhere. * *----------------------------------------RevisionHistory--------------------------------* No Version Date Revised By Item Description * 1 V0.95 08.05.18 WhoAmI abcdefghijklm WhatUDo * ************************************************************************/ /************************************************************************ * Multi-Include-PreventSection ************************************************************************/ #ifndef __FN_FILENAME_H #define __FN_FILENAME_H
    /************************************************************************ * Debugswitch Section ************************************************************************/ #define D_DISP_BASE
    /************************************************************************ * Include File Section ************************************************************************/ #include "IncFile.h"
    /************************************************************************ * Macro Define Section ************************************************************************/ #define MAX_TIMER_OUT (4)
    /************************************************************************ * Struct Define Section ************************************************************************/ typedef structCM_RadiationDose { unsigned charucCtgID; charcPatId_a[MAX_PATI_LEN]; }CM_RadiationDose_st,*CM_RadiationDose_pst;
    /************************************************************************ * Prototype Declare Section ************************************************************************/ unsigned intMD_guiGetScanTimes(void); … … #endif

    2. 源文件书写顺序:

    No. Item 1 Source File Header Section 2 Debug Switch Section 3 Include File Section 4 Macro Define Section 5 Structure Define Section 6 Prototype Declare Section 7 Global Variable Declare Section 8 File Static Variable Define Section 9 Function Define Section

    如:

    /************************************************************************* * File Name : FN_FileName.c * Copyright : 2003-2008 XXXX Corporation, All Rights Reserved. * Module Name : Draw Engine/Display * * CPU : ARM7 * RTOS : Tron * * Create Date : 2003/10/01 * Author/Corporation : WhoAmI/your company name * * Abstract Description : Place some description here. * *-----------------------Revision History--------------------------------* No Version Date Revised By Item Description * 1 V0.95 00.05.18 WhoAmI abcdefghijklm WhatUDo * ************************************************************************/
    /************************************************************************ * Debug switch Section ************************************************************************/ #define D_DISP_BASE
    /************************************************************************ * Include File Section ************************************************************************/ #include "IncFile.h"
    /************************************************************************ * Macro Define Section ************************************************************************/ #define MAX_TIMER_OUT (4)
    /************************************************************************* Struct Define Section ************************************************************************/ typedef struct CM_RadiationDose { unsigned char ucCtgID; char cPatId_a[MAX_PATI_LEN]; }CM_RadiationDose_st, *pCM_RadiationDose_st;
    /************************************************************************ * Prototype Declare Section ************************************************************************/ unsigned int MD_guiGetScanTimes(void);
    /************************************************************************ * Global Variable Declare Section ************************************************************************/ extern unsigned int MD_guiHoldBreathStatus;
    /************************************************************************ * File Static Variable Define Section ************************************************************************/ static unsigned int nuiNaviSysStatus;
    /************************************************************************ * Function Define Section ************************************************************************/

  • 相关阅读:
    控制一个cell不可被移动到另外一个section中
    core data 手动修改 .xcodatamodeld 文件 和 po 生成的 模型类 注意事项
    stringByTrimmingCharactersInSet 取出string 前后空格
    项目架构简述
    nil NULL [NSNULL null]
    如何定义一个应用之间调用的ios 本地URL
    UITableView隐藏多余的分割线
    解决UItableView cell的间隔线 separatorStyle ( plain group 两种类型)
    模拟器 真机 测试 内存消耗 资源对比
    微服务架构:Eureka集群搭建
  • 原文地址:https://www.cnblogs.com/embeddedking/p/9736053.html
Copyright © 2011-2022 走看看