简体   繁体   中英

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.
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:

#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:

#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).

You don't need to have 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.

Let's deconstruct what you're doing when you're calling 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. Notice that the prototype for setTeamA() actually takes in a sTest *p . In your example, you're passing in the teamA field. So what you really want to call is setTeamA(&dataPtr[0], "name1") . This translates to the pointer pointing to the data at dataPtr[0].

While this works, as I said before, the dataPtr is unecessary. So this is equivalent to: 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.

Use 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.

That dataPtr variable is useless here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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