SNPRINTF cannot use a "string pointer" assignment, you can use a character array

Source: Internet
Author: User
#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;}

SNPRINTF cannot use a "string pointer" assignment, you can use a character array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.