自己积累小经验

1.用malloc申请空间及如何引用

void initData(int *array...);

void main(void){
    int *array1;
    array1 = (int *)malloc(sizeof(int) * count);

    initData(array1...);  

}

2.对输出格式进行限制(用条件表达式)

第一个直接输出,其他的输出逗号再输出

void showArray(int *array, int count){
    int i;

    for(i = 0; i < count; i++){
        //printf("%d ", array[i]);
        printf(i == 0 ? "%d": ",%d", array[i]); //对输出格式进行限制
    }
    printf("\n");
}

3.一写malloc 下面立刻写free

4.对错误进行说明

typedef short ErrorNo;
#define NO_ERROR  0
ErrorNo errorNo  = NO_ERROR;   //全局变量

#define ERR_LINEAR_EMPTY             -1
#define ERR_LINEAR_FULL              -2
#define ERR_LINEAR_ILLEAGal_INDEX    -3
#define ERR_LINEAR_NULL_ERROR        -4


const char *linearErrMsg[] = 
{
    "没有错误!",  //char*  linearErrMsg[]  每一个数组的元素都是一个指针,指向一个字符串,不给个数,方便扩充
    "线性表为空!",
    "线性表已满",
    "下标不合法",
    "NULL错误",
}; 

void showLinearErrMsg(ErrorNo errorNo){
    showErrMsg(linearErrMsg, errorNo);
}


void showErrMsg(const char **errMsg, ErrorNo errNo){
    printf("[错误代码:%d]%s\n", -errNo, errMsg[-errNo]);
}

猜你喜欢

转载自blog.csdn.net/weixin_42072280/article/details/83507092