【发布时间】:2009-03-31 06:24:21
【问题描述】:
我有一个包含指针数组的结构。我想以字符串格式插入数组数字,即“1”、“2”等。
但是,使用 sprintf 或 strncpy 有什么不同吗?
我的代码有什么大错误吗?我知道我必须拨打免费电话,我将在我的代码的另一部分中这样做。
非常感谢您的建议!
struct port_t
{
char *collect_digits[100];
}ports[20];
/** store all the string digits in the array for the port number specified */
static void g_store_digit(char *digit, unsigned int port)
{
static int marker = 0;
/* allocate memory */
ports[port].collect_digits[marker] = (char*) malloc(sizeof(digit)); /* sizeof includes 0 terminator */
// sprintf(ports[port].collect_digits[marker++], "%s", digit);
strncpy(ports[port].collect_digits[marker++], digit, sizeof(ports[port].collect_digits[marker]));
}
【问题讨论】: