ポインタの配列を持つ構造体があります。「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]));
}