c - 在 .c 中使用 .h 文件中的结构变量
问题描述
shm_fileuploader.c:
#include "sharedMemory.h"
struct filesharing_struct temp()
{
printf("Enter the name of the file. ");
scanf("%s", &fname);
}
共享内存.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
//void* get();
const int SIZE = 40960;
const char *name = "obaloch_filesharing";
struct filesharing_struct{
char flag;
int size;
char content;
char fname[50];
};
//struct filesharing_struct temp;
我收到一条错误消息,说“错误:'fname' undeclared”,我不知道为什么,因为 fname 在我的头文件中声明。无论如何我可以在 .c 文件中的 .h 文件中使用 fname 吗?
解决方案
您需要声明一个类型为结构的变量,并扫描到其fname
成员。
然后你可以从函数返回结构,以满足返回类型。
struct filesharing_struct temp()
{
struct filesharing_struct temp;
printf("Enter the name of the file. ");
scanf("%s", &temp.fname);
return temp;
}
推荐阅读
- django - 如何在 CharField Django 中放置名称数组
- multithreading - Rust:允许多个线程修改图像(向量的包装器)?
- regex - 在参数中表达嵌套路线?
- javascript - 单击下载按钮时,应出现窗口对话框以选择其下载位置
- python - 如何正则表达式字符串中的数字长度为 7 但该数字在 Python 中不是以 0 开头?
- node.js - MongoDB Atlas数据库vs MongoDB本地,在交易速度方面最适合SaaS(查询)
- python-3.x - 使用 loc 时遇到错误。功能
- jquery - 未捕获的类型错误:无法使用 proengsoft / laravel-jsvalidation 读取未定义的属性“indexOf”
- sql - 带有 as 表达式的 SQL 显示多个结果
- sql-server - 将数据类型 nvarchar 转换为数字时出错