C/C++中利用空指针(NULL),提高程序运行效率

2014-11-24 13:14:00 · 作者: · 浏览: 7
//程序作者:管宁
//站点:www.cndev-lab.com

//所有稿件均有版权,如要转载,请务必著名出处和作者

#include < iostream>
#include < string>
using namespace std;

void print_char( char* array[]); //函数原形声明

void main( void)
{
char* test[]={"abc","cde","fgh",NULL}; //这里添加一个NULL,表示不指向任何地址,值为0
print_char(test);
cin.get();
}

void print_char( char* array[])
{
while(*array!=NULL)
{
cout<<*array++<}
}

这里的写法,可以避免使用for循环,减少栈空间内存的使用和减少运行时的计算开销!

返回管宁的C++教程专题