0

I'm making a program in C. Basically I have a struct that contains some fields, and an array of pointers. Each struct has an pointer array that points to another struct forming a "connection" between them. I'm trying to get the value of a field that is stored at the memory address the pointer that is pointing at it.

Suppose this. I have two of these nodes in memory. A and B. A has a pointer inside A's array that is a reference to B. To get this pointer I'd have to do something like this:

*temp_ptr = (*ptr).pointer_array[0]

This would get the pointer address *ptr and give it to *temp_ptr.

Now what I am wondering is this. How can i do this? When I try this, I get "Expression must have struct or union type"

When I try this in lets say Java I could do this

int variable = field[0].fieldIWantToGet

I'd get the desired outcome.

Heres an image to clarify the intended behaviour that I'm trying to get. Link to behavior

Where Struct A is in a "global" collection of structs and has a array of pointers that lead to other Structs, such as B

Here is some code from my project.

#define GLOBAL_PTR_ARRAY_SIZE 10

Node* global_node_array[10];  

typedef struct Node{

unsigned char node_id;
int *ptr_array[10];
int ptr_array_size;


}Node;



void append_connection(short position, short destination) {   

    Node* position_ptr = global_node_array[position];        
    Node* destination_ptr = global_node_array[destination];

    if ((*position_ptr).ptr_array_size < GLOBAL_PTR_ARRAY_SIZE) {   
        int current_ptr_array_size = (*position_ptr).ptr_array_size;  

        (*position_ptr).ptr_array[current_ptr_array_size] = destination_ptr;

        (*position_ptr).ptr_array_size++;

}

void print_id(Node* ptr) {

    node* dptr = NULL;

    dptr = ptr->ptr_array[0];

    pptr = (int) (*ptr).ptr_array[0];

    fprintf(stdout, "%d connection to %d exists", (*ptr).node_id, dptr- 
   >node_id);      
}


int main(int argc, char const *argv[])
{
append_connection(0,1);
print_id(global_node_array[0]);

return 0;
}
2
  • (*ptr).pointer_array[0] can be simplified to ptr->pointer_array[0] Commented Feb 24, 2019 at 18:36
  • "This would get the pointer address *ptr and give it to *temp_ptr." Why do you think this? There is more on the right-hand side than just *ptr, and you've ignored those extra bits. Please present your minimal reproducible example. Commented Feb 24, 2019 at 18:38

1 Answer 1

2

Your picture shows array of the structs not the pointers. But the example below covers both.

struct a{
    int field1,field2;
}

struct b{
    struct a m[10];
}

struct c{
    struct a *m[10] 
}




   /* and usage */        
    struct c *x;
    struct b *y;

    x -> m[5].field1;
    y -> m[5] -> fileld1;
Sign up to request clarification or add additional context in comments.

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.