无法读取C中的结构数组

时间:2016-01-21 18:03:43

标签: c arrays structure

我正在尝试阅读下面代码中的对象成员。 问题是代码无法读取数组中的第二个成员(car [i] .model)和第三个成员(car [i] .price),只读取第一个成员(car [i] .manufacturer)。

#include <stdio.h>
#include <conio.h>

struct machine
{
    int price;
    char manufacturer[30];
    char model[30];
};


int main()
{
    int i = 0, n;
    printf("Introduce number of cars: ");
    scanf_s("%d", &n);

    struct machine car[100];

    for (i = 0; i < n; i++)
    {
        printf("Data of the car nr. %d:\n", i+1);
        printf("Manufacturer: ");
        scanf_s("%s", car[i].manufacturer);
        printf("Model: ");
        scanf_s("%s", car[i].model); printf("\n");
        printf("Price: ");
        scanf_s("%d", &car[i].price); printf("\n");
    }
    for (i = 0; i < n; i++)
    {
        printf("Data of the car nr. %d:\n", i + 1);
        printf("Manufacturer: %s\n", car[i].manufacturer);
        printf("Manufacturer: %s\n", car[i].manufacturer);
        printf("Model: %s\n", car[i].model);
        printf("Price %d\n", car[i].price);
    }


    _getch();
}

1 个答案:

答案 0 :(得分:1)

cellMeansDF <- aggregate(score ~ gene, df, mean) require(ggplot2) plot1 <- ggplot(cellMeansDF, aes(gene, score)) + geom_bar(stat = "identity", position = "dodge", size = 0.1) + xlab("") + theme(axis.text.x = element_text(face = "plain", color = "black", size = 15)) plot1 要求为格式为scanf_s的输入参数指定缓冲区大小。缓冲区大小包括终止空值。像这样调整你的代码:

%s