snprintf不能使用"字符串指针"赋值,可以使用字符数组
#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;
}
muahao@aliyun.com
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步