C程序跳过字符串中的第一个字符

时间:2016-11-29 16:32:52

标签: c struct printf scanf

正常工作,但跳过字符串名称中的第一个字符。任何人都可以解释我没有看到的内容吗?

#include "stdio.h"
/*
*BN
*/
int main(void) {
// Disable stdout buffering
//setvbuf(stdout, NULL, _IONBF, 0);

struct data {
  char name[40];
  char street[50];
  char city[30];
  char state[3];
  char zip[6];
  float salary;
};

struct data p = {
scanf("%s %s %s %s %s %f", &p.name, &p.street, &p.city, &p.state, &p.zip, &p.salary)
};

printf(" Name:%s\n Street:%s\n City:%s\n State:%s\n Zipcode:%s\n Salary:%.2f", p.name, p.street, p.city, p.state, p.zip, p.salary);
return 0;
}

2 个答案:

答案 0 :(得分:2)

将结构初始化为struct data p;,然后执行scanf

struct data p;
scanf("%s%s%s%s%s%f", &p.name, &p.street, &p.city, &p.state, &p.zip, &p.salary);

You can check some struct stuff here.

答案 1 :(得分:2)

嗨,你错误地初始化你的结构

使用此:

#include "stdio.h"
/*
*BN
*/
int main(void) {
    // Disable stdout buffering
    //setvbuf(stdout, NULL, _IONBF, 0);

    struct data {
      char name[40];
      char street[50];
      char city[30];
      char state[3];
      char zip[6];
      float salary;
    };

    struct data p;//Dont Initialize it here!
    printf("Please Enter Name Street City State Zip Salary:");
    scanf("%s %s %s %s %s %f", &p.name, &p.street, &p.city, &p.state, &p.zip, &p.salary);

    printf(" Name:%s\n Street:%s\n City:%s\n State:%s\n Zipcode:%s\n Salary:%.2f\n", p.name, p.street, p.city, p.state, p.zip, p.salary);
    return 0;
}