【发布时间】:2010-08-27 14:03:56
【问题描述】:
我试图在编译时通过传递来定义路径:
-DDCROOTDEF='"/path/to/stuff"'
在编译行。然后我尝试在代码中使用它:
char * ptr_path;
strcpy(ptr_path, DCROOTDEF);
strcat(ptr_path,"/MainCommons/CommonLib/fonts/Arial.ttf");
char *pftf=ptr_path;
gdImageStringFT(pimg,brect,iclr,pftf,pts,ang,ixp,iyp, (char *)cbuf);
这给了我一个分段错误。但是,如果我尝试先打印字符串:
char * ptr_path;
strcpy(ptr_path, DCROOTDEF);
strcat(ptr_path,"/MainCommons/CommonLib/fonts/Arial.ttf");
char *pftf=ptr_path;
printf("%s\n",pftf);
gdImageStringFT(pimg,brect,iclr,pftf,pts,ang,ixp,iyp, (char *)cbuf);
它工作得很好。我在这里错过了什么复杂的 char 指针?
谢谢
【问题讨论】:
-
在字符串末尾加一个'\0'怎么样?
-
@karlphillip:
\0自动包含在字符串文字的末尾。