c - 在结构不是全局的情况下访问结构

标签 c arrays struct

我有一个名为members的结构,其中包含一堆字符数组和整数。该结构已在 Header.h 中声明,并由 main 内的 source.c 中的“结构成员 pt”定义。从这里开始,for 循环运行 5 次,并将变量添加到 pt[x] 中的字符数组和整数中。 。 现在我需要能够从名为 void search(int a); 的函数访问它(可能不应该是一个 void,因为我希望它返回一个值。但我稍后会修复这个问题) void search 应该做的基本上是

int willReturn[10];
int b = 0;
for(int x = 0; x<a; x++)
{
    if(pt[x].hasPayed == 0)
    {
        willReturn[b] = x;
        b++;
    }
}

该代码可能有问题,但我需要知道的是如何访问 pt[x].hasPayed 。 有任何想法吗? 我不想使用任何全局变量。 预先感谢您。

最佳答案

下面的示例代码可能会对您有所帮助。

header.h

struct members {
    int hasPayed;
};


ma​​in.c

#include <stdio.h>
#include <string.h>
#include "main.h"

typedef struct members MEMBERS;

void print_member(MEMBERS *pt) {
    int i;
    for(i =0 ; i< 10; i++)
    {
        printf(" %d\n",pt[i].hasPayed);
    }

}

void main () {
    MEMBERS pt[10];
    int i;
    for(i =0 ; i< 10; i++)
    {
        pt[i].hasPayed= i;
    }
    print_member(pt);
}

代替 print_member 编写您的搜索逻辑。

关于c - 在结构不是全局的情况下访问结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22558551/

相关文章:

c++ - 如何初始化unsigned char指针

c - 如何从用户空间程序访问 "Permission denied"file/sys/class/gpio/gpio2/direction

c++ - 不能在派生模板类中使用 struct 吗?

c++ - 使用 C++ 读回时将结构写入文件但值错误

c - 打开命名管道的问题

python - 从 Python 调用 C DLL 函数时出错

android - 用 numpy 保存二进制文件以供读取的最安全方法

java - 如何对二维数组进行升序排序? java

javascript - 使用 map 函数从包含数组的对象创建一个数组

c++ - 如何创建另一个类结构的 vector ? (C++ 11)