c - 名字不会显示

标签 c pointers struct printf

我的代码编译没有问题,接受输入没有问题,打印没有问题。但是,它不想打印名称,它只打印一个空格。它正在工作,我做了一些额外的更改,但我不知道出了什么问题。任何想法将不胜感激!

#include <stdio.h>
#include <stdlib.h>

struct File {
char type;
char name;
int time;
int size;
}f;

int main()
{
struct File * c = malloc(1 *sizeof(struct File));
printf("Enter file name: \n");
scanf("%s", &f.name);
printf("Enter the file size: \n", f.size);
scanf(" %d", &f.size);
printf("Enter when the file was last accessed: \n", f.time);
scanf(" %d", &f.time);
printf("Enter the file type: \n", f.type);
scanf("%s", &f.type);
printf("\n");
structPrint();
}

structPrint()
{
printf("Filename: %s, File Size: %d, Type: [%s], Access Time: %d \n", &f.name, f.size,     &f.type, f.time);
}

最佳答案

您的结构恰好包含 1 个字符 type 和 1 个字符 name 的空间。这两个都可能比单个字符长 - 事实上,它们必须,因为它们大概应该是以 null 结尾的字符串。尝试将它们制作成数组...

关于c - 名字不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709418/

相关文章:

struct - 如何在 Julia 中引用结构本身

在链表中创建数据类型为 "struct"的新节点

c - Qt C 错误重复符号 _function_names in juicy_lucy.o 和 main.o for architectures x86_64

c++ - C++中指向int的指针

c++ - 函数中的内存释放 (C++)

c - 如何从 void 函数中检索数组并传递给 Main 中的其他函数?

php - 在 PHP 中获取内核结构大小的可靠方法

Java 编译器目标代码(字节代码?)

sql - SQL 中存在多个相同值的数据时,仅更改列的一个值

c - 将整数向下舍入到最接近的 4 倍数的标准方法