zoukankan      html  css  js  c++  java
  • 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
    ", t1->name);
    	printf("t2:%s
    ", t2.name);
    
    	/*snprintf不能使用字符串指针赋值*/
    	char *a;
    	char *dev = t1->name;
    	/*可以使用字符串数组*/
    	//char dev[10];
    	//strcpy(dev, t1->name);
    
    	printf("dev:%s
    ", dev);
    	sprintf(a, "/dev/%s", dev);
    	printf("a:%s
    ", a);
    
    	return 0;
    }
    
  • 相关阅读:
    20210131
    20210130
    20210129
    20210128
    20210127
    例3-7
    例3-5
    例3-4
    例3-3
    例3-2
  • 原文地址:https://www.cnblogs.com/muahao/p/8258875.html
Copyright © 2011-2022 走看看