阅读背景:

snprintf()使用Warn提示:warning: format not a string literal and no format arguments

来源:互联网 

问题:

使用snprintf()完成字符串的复制操作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ARR_SIZE(a)		(sizeof((a))/sizeof((a)[0]))
#define LEN_BUF			5

int main()
{
	char buf[] = "0123456789";
	char buf1[LEN_BUF];
	char buf2[LEN_BUF];
	// char *buf2 = NULL;

	// Source buf greater than dest buf1 causes stack overflow
	// strcpy(buf1, buf);
	// printf("buf1 is: %s\n", buf1);

	// buf2 = calloc(LEN_BUF, sizeof(char));
	memset(buf2, 0, LEN_BUF);
	strncpy(buf2, buf, LEN_BUF);
	printf("buf2 is: %s\n", buf2);
	// free(buf2);

	memset(buf2, 0, LEN_BUF);
	snprintf(buf2, LEN_BUF, buf);
	printf("buf2 is: %s\n", buf2);

	return 0;
}
#include <st



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: