0

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

2
  • 1
    Why the C++ tag? I don't see anything C++ here, the file name is also .c. Commented Oct 25, 2019 at 16:43
  • 1
    In order to get access to the functions from main, you need to #include "functions.h" just like functions.c has done. This way, when the compiler processes main.c it knows to also process functions.h, and so it learns about those extra functions. Otherwise you will get some interesting warnings and errors perhaps when you compile main.c Commented Oct 25, 2019 at 16:46

2 Answers 2

2

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 sTests, 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.

Sign up to request clarification or add additional context in comments.

1 Comment

oops you're right. Editing my answer on that part, but everything else should be correct
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.