0

What's the correct way of accessing (with a pointer) a variable within a struct within an array within a struct?

I's like to get to variables x and y within position2D with a pointer from function()? Note that I'm traversing the nodes (and the points) in function(), and was hoping to write something like:

draw_point(p->vertices[i]->x, p->vertices[i]->y);

but that doesn't seem to work.

typedef struct Position2D{
  uint8_t x;
  uint8_t y;
} position2D;

typedef struct Node{
  int num;
  position2D vertices[4];
  struct Node *next;
} node;

/* initialisation: */

node *next1 = NULL; //should be empty
node node1 = {1, {{0,0}, {5,0}, {5,5}, {0,5}}, &next1};
node *next0 = &node1;
node node0 = {0, {{0,10}, {10,10}, {10,15}, {0,15}}, &next0};
node *start = &node0;

/*traverse all nodes and their inner vertices arrays: */
void function(void){
node *p;
for(p = start; p != NULL; p = p->next){
  int i;
  for (i=0; i<4; i++){ //traverse their four points
    //How to get to the x and y at this line?
  }
}
4
  • 2
    "but that doesn't seem to work." What errors do you see? Commented Feb 22, 2018 at 15:45
  • 1
    Think about types and which operator is resulting in what type, and which operator can be applied to this type. Commented Feb 22, 2018 at 15:45
  • 1
    From a quick glimpse: In draw_point(p->vertices[i]->x, p->vertices[i]->y); I think you need to replace the arrows -> after [i] with . so it becomes draw_point(p->vertices[i].x, p->vertices[i].y); Commented Feb 22, 2018 at 15:46
  • @BjornA. Although compiled, didn't run properly, embedded environment. ;) I've tried with draw_pixel(p->vertices[i].x, p->vertices[i].y); which also compiles, but now one of my for loops seem to go on forever... :/ Commented Feb 22, 2018 at 15:55

1 Answer 1

1

vertices is a normal structure variable, not of struct pointer type. While accessing x and y use dot . operator instead of -> operator

Replace below statement

draw_point(p->vertices[i]->x, p->vertices[i]->y);

with

 draw_point(p->vertices[i].x, p->vertices[i].y);

EDIT : Another problem in your code while assigning next field.

node node1 = {1, {{0,0}, {5,0}, {5,5}, {0,5}}, &next1};

should be

node node1 = {1, {{0,0}, {5,0}, {5,5}, {0,5}}, (struct Node*)next1};

Here is the working code

#include<stdio.h>
typedef struct Position2D{
        int x;
        int y;
} position2D;

typedef struct Node{
        int num;
        position2D vertices[4];
        struct Node *next;
} node;
/*traverse all nodes and their inner vertices arrays: */
void function(void){
/* initialisation: */
        node *next1 = NULL;
        node node1 = {1, {{0,0}, {5,0}, {5,5}, {0,5}}, (struct Node*)next1};
        node *next0 = &node1;
        node node0 = {0, {{0,10}, {10,10}, {10,15}, {0,15}}, (struct Node*)next0};
        node *start = &node0;
        node *p = NULL ;
        int i=0;
        for(p=start;p!=NULL;p=p->next) {
                for (i=0; i<4; i++){ //traverse their four points
                        printf("%d %d \n",p->vertices[i].x, p->vertices[i].y);
                }
        }

}
int main() {
        function();
        return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it compiles. (But hangs inside the function(), perhaps my linked list isn't set up properly...)
I've narrowed it down to my outer for loop, it seems to go on forever.
Hey @eriksan have you got where mistake was there ? If not let me know I can explain.
Thanks for spotting the error with assigning the next filed, I'll try it out now!

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.