C 语言下 BOOL TRUE FALSE的使用

来源:互联网 发布:淘宝怎么设置下架时间 编辑:程序博客网 时间:2024/05/06 19:57
<pre name="code" class="cpp">#include <stdio.h>#include <stdlib.h>#define BOOL int    // C语言 巧妙的使用 宏来解决C语言不支持 BOOL类型的问题 #define TRUE 1     // C语言 巧妙的使用 宏来解决C语言不支持 TRUE类型的问题 #define FALSE 0   // C语言 巧妙的使用 宏来解决C语言不支持 FALSE类型的问题 struct array{int count;int size;char *pBase;};void init_arr (struct array *pArr,int number);void show_arr (const struct array *pArr);BOOL is_empty (const struct array *pArr);int main (void){struct array arr;init_arr (&arr,10);show_arr (&arr);return 0;}void init_arr (struct array *pArr,int number){pArr->pBase = (char *)malloc(sizeof(char)*number);if (NULL == pArr->pBase){printf ("Memory allocation failed!\a\n");exit(EXIT_FAILURE);}else{pArr->size = number;pArr->count = 0;}return;}void show_arr (const struct array *pArr){int i;if ( is_empty(pArr) )printf ("Array is empty!\a\n");else{for (i=0;i<(pArr->count);i++)printf ("%c ",pArr->pBase[i]);printf ("\n");}return;}BOOL is_empty (const struct array *pArr){if (pArr->count == 0)return TRUE;elsereturn FALSE;}


                                             
0 0