如何初始化一个字符指针,然后使用 sprintf 创建一个字符串

How to initialise a char pointer and then create a string with it using sprintf

我正在创建一个以特定格式输出当前系统时间的函数。

char *get_time() {
    char *current_time;
    time_t rawtime;
    struct tm *timeinfo;

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    sprintf(current_time, "[%d/%d %d:%d] # ", timeinfo->tm_mday, timeinfo->tm_mon + 1, timeinfo->tm_hour, timeinfo->tm_min);

    return current_time;
}

char *current_timesprintf()中使用前需要初始化。我该怎么做?

char *current_time = (char *) malloc(17) 为最大大小 current_time 和空终止符分配足够的内存。

从 C99 开始,复合文字 为内存分配提供了一个绝妙的解决方案。 get_time() 中的 return 值在块结束前有效

#include <stdio.h>
#include <time.h>
#define GET_TIME_N 17
#define GET_TIME()  get_time((char[GET_TIME_N]) { ""}, GET_TIME_N)

char *get_time(char *current_time, size_t n) {
  time_t rawtime;
  struct tm *timeinfo;
  time(&rawtime);
  timeinfo = localtime(&rawtime);
  snprintf(current_time, n, "[%d/%d %d:%d] # ", timeinfo->tm_mday,
          timeinfo->tm_mon + 1, timeinfo->tm_hour, timeinfo->tm_min);
  return current_time;
}

int main(void) {
  printf("%s\n%s\n%s\n", GET_TIME(), GET_TIME(), GET_TIME());
}

输出

[27/3 12:33] # 
[27/3 12:33] # 
[27/3 12:33] # 

如果无法确定进入目标字符串的字符数量,snprintf() 允许通过额外的 "fake" 调用来计算必要的大小。

来自 C11 草案:

int snprintf(char * restrict s, size_t n, const char * restrict format, ...);

The snprintf function returns the number of characters that would have been written had n been sufficiently large, not counting the terminating null character, or a negative value if an encoding error occurred.

示例:

char * unknown_sized_string(const char * str)
{
  char * buffer = NULL;

  int size = snprintf(buffer, 0, 
    "I do not known how long this will be: %s", str);

  if (0 <= size)
  {
    buffer = malloc(size + 1);

    if (NULL != buffer)
    {
      snprintf(buffer, size,
        "I do not known how long this will be: %s", str);
    }
  }

  return buffer;
}