#cat snprintf.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student{
	int age;
	char *name;
};

int main(void)
{
	/*t1 结构体指针*/
	struct student *t1;
	t1 = malloc(sizeof(struct student));
   	t1->age = 11;
	t1->name = "ahao.mah";

	/*t2 结构体变量*/
	struct student t2;
	t2.name = "jack";
	t2.age = 22;

	printf("t1:%s\n", t1->name);
	printf("t2:%s\n", t2.name);

	/*snprintf不能使用字符串指针赋值*/
	char *a;
	char *dev = t1->name;
	/*可以使用字符串数组*/
	//char dev[10];
	//strcpy(dev, t1->name);

	printf("dev:%s\n", dev);
	sprintf(a, "/dev/%s", dev);
	printf("a:%s\n", a);

	return 0;
}

相关文章:

  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2021-12-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-07-12
  • 2021-10-19
  • 2021-12-03
相关资源
相似解决方案