Share typedef struct array used in file among other files

100 views Asked by At

I am not been able to share a typef struct among files, concretely from one file and the main.c. To summarize I have the following:

pagos.h:

    typedef struct
{
    uint8_t usuario[LONGITUD_USUARIO];
        uint8_t importe[LONGITUD_IMPORTE];
        uint8_t fecha[LONGITUD_TIMESTAMP];
        bool        posicion_ocupada;

}trama_tpv_t;

pagos.c:

#include "pagos.h"
trama_tpv_t trama_tpv;
trama_tpv_t trama_tpv;
trama_tpv_t array_trama_tpv[TAMCOLAPAGOS];

I use the trama_tpv and the array_trama_tpv[TAMCOLAPAGOS] here, because I fill the array with trama_tpv by using some functions.

main.c:

extern trama_tpv_t array_trama_tpv[TAMCOLAPAGOS];

Edited: It's a bit complicated to explain how I fill the array because I collect Bluetooth frames in pagos.c. This is a code which compiled serves as a firmware for a nRF51 Bluetooth Development kit. The thing is that I get the info in pagos.c because I am able to print importe and usuario using my own lib in order to print on a LCD-TFT display. The problem is that I need to get the values of the array in order to print the array with the data stored in the array, and I use the bool posicion_ocupada to know which array position is aviable to be written. The problem is not about the target of the code, but how can I share a typedef array struct, because in Java is enough using a public as you create the objetc but in C I must be missing something. In order to link and compile I use Keil, which should be doing a good job. Anyway I am trying also coding an isolated example in XCODE and the result is the same, I can share bool, int... but not an array of structs:

typedef struct
{
    bool        posicion_ocupada;

}trama_tpv_t;

ISOLATED EXAMPLE: main.c:

#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include "test.h"



int main(){


init();
change();


    for (int i=0; i<9; i++) {
        if(array_trama_tpv[i].posicion_ocupada)
            printf("\nel array es true");
        else
            printf("\nel array es false");
    }


printarray(); /*Used only because I though that maybe calling a function which is located into test.c it could be able to access the values I want from the array.*/

return(0);

test.c:

       #include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include "test.h"
//#include "global.h"


trama_tpv_t trama_tpv;
trama_tpv_t array_trama_tpv[9];


void init(void){
    for (int i=0; i<9; i++) {
        array_trama_tpv[i].posicion_ocupada=false;
    }



}

void change(void){
    for (int i=0; i<9; i++) {
        if (i<6) {
             array_trama_tpv[i].posicion_ocupada=true;
        }
        else{
        array_trama_tpv[i].posicion_ocupada=false;
    }

}
}

void printarray(void){
    for (int i=0; i<9; i++) {
        if (array_trama_tpv[i].posicion_ocupada) {
            printf("\nes true");
        }
        else
        {
           printf("\nes false");
        }
    }

}

test.h:

    #ifndef __bittest__test__
#define __bittest__test__

#include <stdio.h>
#include <stdbool.h>
#include <time.h>


typedef struct
{
    bool        posicion_ocupada;

}trama_tpv_t;

extern trama_tpv_t array_trama_tpv[9];



void init(void);

void change(void);
void printarray(void);


#endif /* defined(__bittest__test__) */

The output I get is the following:

    el array es true
el array es true
el array es true
el array es true
el array es true
el array es true
el array es false
el array es false
el array es false
es true
es true
es true
es true
es true
es true
es false
es false
es false

The question is why am not having the same result if it should be the same array. Is this possible? init() should have set all the booleans to false but it is not acting like this.


And I try to use it in a function. So I checked setting some breakpoints that array_trama_tpv[0].posicion_ocupada is set to true, but when I go along the array and print all the posicion_ocupada values of the array, all of them are false. Curiosly, I am able to share variables such an int, bool or something like that, but with this I am not able to share the values.

Thank you in advance. I have checked and tried many post from here, so sorry if I missed something.

Regards,

Iván

0

There are 0 answers