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

来源:互联网 发布:淘宝无线链接转换工具 编辑:程序博客网 时间:2024/05/15 06:44
//程序作者:管宁
//站点: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++<<endl;
}
}

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