《C现代编程》---读书笔记(1)

第3章 C语言与面向对象

3.2.1 C与模块化

以经典栈的实现为例,代码如下:

#ifndef _STACK_H_
#define _STACK_H_

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    int top;
    const size_t size;
    int * const pBuf;
} Stack;

bool push(Stack *p, int val);
bool pop(Stack *p, int *pRet);

#define newStack(buf) {                 \
    0, sizeof(buf) / sizeof(int), (buf) \
} 

#ifdef __cplusplus
}
#endif

#endif

//stack.c
#include <stdbool.h>
#include "stack.h"

static bool isStackFull(const Stack *p) {
    return p->top == p->size;
}

static bool isStackEmpty(const Stack *p) {
    return p->top == 0;
}

// true: 成功, false: 失敗
bool push(Stack *p, int val) {
    if (isStackFull(p)) return false;
    p->pBuf[p->top++] = val;
    return true;
}

// true: 成功, false: 失敗
bool pop(Stack *p, int *pRet) {
    if (isStackEmpty(p)) return false;
    *pRet = p->pBuf[--p->top];
    return true;
}

  • 在函数名和变量名前面制定static修饰符,使函数和变量只在该编译单位内部有效,可以避免命名冲突,如下:
static bool isStackFull(const Stack *p) {
    return p->top == p->size;
}

static bool isStackEmpty(const Stack *p) {
    return p->top == 0;
}

  • 使用newStack的宏可以方便地将结构体初始化,如下:
#define newStack(buf) {                 \   
    0, sizeof(buf) / sizeof(int), (buf) \
}

//使用宏初始化
int buf[16];
Stack stack = newStack(buf);

//宏展开
Stack stack = {0, sizeof(buf)/sizeof(int), (buf)};
  • 使用extern "C"可使C++顺利调用C函数,同时#ifdef __cplusplus是为了告诉编译器,extern "C" 仅在C++中编译时才有效。C++标准规定了在C++中编译时,__cplusplus标示符才会被定义,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值