首页 > 解决方案 > 在 .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 吗?

标签: c

解决方案


您需要声明一个类型为结构的变量,并扫描到其fname成员。

然后你可以从函数返回结构,以满足返回类型。

struct filesharing_struct temp()
{
    struct filesharing_struct temp;

    printf("Enter the name of the file. ");
    scanf("%s", &temp.fname);
    return temp;
}

推荐阅读