My unit doesn't teach this so I am unaware about the correct syntax to go about what I want to do. I have hit google but have produced no answers to my specific problem. So here we go.

I have a series of nested structs.

Is this possible that instead of typing - program.week[0].workout[0].exercise[0].exerciseName
everytime I want to access an element.
That I can access the elements of the most nested struct via a pointer.

Eg.

clientProgram *prtToWorkout;
prtToWorkout = &(myProgram.week[0].workout[wkOut]);

PopulateWorkout(&prtToWorkout, numberOfExercises); //Funct call

void PopulateWorkout(clientProgram *prtToWorkout, int numOfEx)
{
    printf("Enter program grouping letter eg. A: ");
    scanf("%c%*c", &(*prtToWorkout)->programName.exercise[ex_num].exerciseName);
}

Hope you guys can help shine some light on this one for me..

Regards,
Nate

Recommended Answers

All 3 Replies

Oops, misunderstood your problem.

This post can be deleted.

Is this possible that instead of typing - program.week[0].workout[0].exercise[0].exerciseName
everytime I want to access an element.
That I can access the elements of the most nested struct via a pointer.

Yes. If you have an object, you can have a pointer to it:

#include <stdio.h>

struct A
{
    struct B
    {
        struct C
        {
            int dat;
        } c;
    } b;
};

int main()
{
    struct A a;
    struct C* pc = &a.b.c;

    pc->dat = 123;
    printf("through struct pointer: %d\n", pc->dat);
    printf("direct access:          %d\n", a.b.c.dat);

    return 0;
}

Is that what you were trying to do?

I haven't got a chance to try it before going to bed.. (and I don't want to be half asleep having coding dreams, like when I code to late in the evening).

BUT it looks like exactly the clarification I was looking for. Thanks heaps.. your reply was simple and effective. Seriously I really appreciate it.

Regards,
Nate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.