简体   繁体   English

C 声明结构数组/指向结构数组的指针

[英]C Declare array of struct/pointer to array of structs

I have an example tutorial with 2 files, "functions.c" and "functions.h" which contain the prototypes and the body of the functions.我有一个示例教程,其中包含 2 个文件“functions.c”和“functions.h”,其中包含函数的原型和主体。
In the example there isn't the main that containing the declaration of the array of struct/pointer to array of structs and the calls to the functions.在示例中,没有包含结构数组的声明/指向结构数组的指针和对函数的调用的主要内容。

functions.c:功能。c:

#include "functions.h"

const char *getTeamA(const sTest *p)
{
    return p->teamA;
}

void setTeamA(sTest *p, char *s)
{
    strcpy(p->teamA, s);
}

int getNum(const sTest *p)
{
    return p->num;
}

void setNum(sTest *p, int i)
{
    p->num = i;
}

functions.h:函数.h:

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

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

#define MAX_CHAR 20
#define SIZE 5

typedef struct {
    char teamA[MAX_CHAR];
    int num;
    // ...
} sTest;


const char *getTeamA(const sTest *p);
void setTeamA(sTest *p, char *s);

int getNum(const sTest *p);
void setNum(sTest *p, int i);

#endif /* FUNCTIONS_H_ */

So my question is所以我的问题是
How can i declare the struct according to the code written above?如何根据上面编写的代码声明结构? So for example:例如:

int main()
{
    sTest data[SIZE];       //size isn't important
    sTest *dataPtr = data;

    setTeamA(dataPtr[0].teamA, "name1");

    // ...

    printf("%d", getNum(dataPtr[1].num)); // just an example. i know that it isn't initialized

    // ...

    return 0;
}

Is this the correct way?这是正确的方法吗? Or is there a better way to declare variables and pass them to the functions?或者有没有更好的方法来声明变量并将它们传递给函数?
The important thing is that i have to stick to the code written in functions.c and functions.h, so the functions cannot directly modify the struct data, you need to use pointers (because there are member selection operator "->" in functions.c).重要的是我必须坚持写在functions.c和functions.h中的代码,所以函数不能直接修改结构数据,需要使用指针(因为函数中有成员选择运算符“->” 。C)。

You don't need to have dataPtr .你不需要有dataPtr You can do the exact same thing by doing data[i] , since you declared data as an array of sTest s, and so data points to the first element in the array.您可以通过执行data[i]来执行完全相同的操作,因为您将 data 声明为sTest的数组,因此data指向数组中的第一个元素。

Let's deconstruct what you're doing when you're calling setTeamA(dataPtr[0].teamA, "name1") .让我们解构您在调用setTeamA(dataPtr[0].teamA, "name1")时所做的事情。 You're trying to set the first sTest struct in the data array to have "name1" as the teamA field.您正在尝试将数据数组中的第一个 sTest 结构设置为将“name1”作为 teamA 字段。 Notice that the prototype for setTeamA() actually takes in a sTest *p .请注意, setTeamA()的原型实际上接受了sTest *p In your example, you're passing in the teamA field.在您的示例中,您传递的是teamA字段。 So what you really want to call is setTeamA(&dataPtr[0], "name1") .所以你真正想要调用的是setTeamA(&dataPtr[0], "name1") This translates to the pointer pointing to the data at dataPtr[0].这转换为指向 dataPtr[0] 处数据的指针。

While this works, as I said before, the dataPtr is unecessary.正如我之前所说,虽然这有效,但 dataPtr 是不必要的。 So this is equivalent to: setTeamA(&data[0], "name1") .所以这相当于: setTeamA(&data[0], "name1")

Also worth noting, you can simply write: setTeamA(data, "name1") since data is already a pointer to the first element in the array.另外值得注意的是,您可以简单地编写: setTeamA(data, "name1")因为 data 已经是指向数组中第一个元素的指针。

Use setTeamA(&data[0], "name1")使用 setTeamA(&data[0], "name1")

It indexes to index 0 and then takes the reference (which is a pointer) of the result therefore making the type an sTest* then the setTeamA function will do it's job setting the teamA field.它索引到索引 0,然后获取结果的引用(这是一个指针),因此将类型设置为 sTest* 然后 setTeamA function 将完成设置 teamA 字段的工作。

That dataPtr variable is useless here.那个 dataPtr 变量在这里没用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM