【发布时间】:2013-01-25 06:59:37
【问题描述】:
我对 C 很陌生,我在使用 strncpy 函数时遇到了困难。\
这是我正在使用的示例:
int main()
{
const char *s = "how";
struct test {
char *name;
};
struct test *t1 = malloc(sizeof(struct test));
strncpy(t1->name, s, sizeof(*s));
t1->name[NAMESIZE] = '\0';
printf("%s\n", t1->name);
}
我有一个 const char *,我需要将 test 的“名称”值设置为 const char。我很难弄清楚这一点。这甚至是正确的方法吗?
非常感谢!
【问题讨论】:
-
@KerrekSB:说得很好:D