Using PIC PORT address in a struct

1.2k views Asked by At

I was trying to pass PORTs address trough a struct by using a function, by i don't know how to correct work with pointers. Here's the code of my struct and function:

typedef struct {
read:1;
last_read:1;
changed:1;
unsigned short *port;    //Here the declaration of the pointer that will receive the address
pin:1;
active_state:1;
} Input;

void Setup_input(Input s,char *port, char pin, char active_state){
 s.port = &port;        //HERE I TRY TO PASS THE ADDRESS OF THE PORT TO THE POINTER OBJECT
 s.pin = pin;
 s.active_state = active_state;

It turns out that I'm not doing it correctly and I'm not able to read or control correctly the PORT. I'm using Mikroelectronic PRO compilers.

1

There are 1 answers

4
harper On BEST ANSWER

This line of code

s.port = &port;

stores the address of the parameter into the member port. When you dereference the pointer in the structure you will access the stack memory where the parameter port were while calling Setup_input(). This causes undefined behavior.

What you obviously want is to assigne the value of the parameter:

s.port = port;