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