const函数参数优点

来源:互联网 发布:淘宝网有没有海外版 编辑:程序博客网 时间:2024/04/30 23:23
const char *
debug_location(file, line, func)
const char *file;// 传入指针 函数内部有机会修改指针内存内容,所以加上const
int line; // 函数内部无法修改该值,所有没有const
const char *func;// 同第一个参数
{
static char buf[1024];// 不用每次调用函数,函数分配
const char *p;


/* truncate pathname */
p = strrchr(file, '/');
if (p)
p++;
else
p = file;


if (func)
snprintf(buf, sizeof(buf), "%s:%d:%s()", p, line, func);
else
snprintf(buf, sizeof(buf), "%s:%d", p, line);


return buf;
}
原创粉丝点击