|
|
View previous topic :: View next topic |
Author |
Message |
spilz
Joined: 30 Jan 2012 Posts: 219
|
Functions and structure, need help :( |
Posted: Mon Jun 15, 2020 7:19 am |
|
|
hello,
I'm trying to deal with structure and functions, but this time I don't understand what is wrong …
exemple code :
Code: | #include <18F66J94.h>
#device ADC=12
#FUSES NOWDT //No Watch Dog Timer
#use delay(clock=64MHz,crystal=16MHz,USB_FULL)
#include <usb_cdc.h>
typedef struct {
int value1;
int value2;
int value3;
int value4;
int value5;
int value6;
int value7;
int value8;
int value9;
int value10;
int16 array1[10];
int16 array2[10];
int16 array3[10];
} testStructure;
void debugState(testStructure struct_loc0){
int i;
if(usb_cdc_carrier.dte_present){
printf(usb_cdc_putc,"\r\n");
printf(usb_cdc_putc,"Valu1 : %3u\r\n",struct_loc0.value1);
printf(usb_cdc_putc,"Valu2 : %3u\r\n",struct_loc0.value2);
printf(usb_cdc_putc,"Valu3 : %3u\r\n",struct_loc0.value3);
printf(usb_cdc_putc,"Valu4 : %3u\r\n",struct_loc0.value4);
printf(usb_cdc_putc,"Valu5 : %3u\r\n",struct_loc0.value5);
printf(usb_cdc_putc,"Valu6 : %3u\r\n",struct_loc0.value6);
printf(usb_cdc_putc,"Valu7 : %3u\r\n",struct_loc0.value7);
printf(usb_cdc_putc,"Valu8 : %3u\r\n",struct_loc0.value8);
printf(usb_cdc_putc,"Valu9 : %3u\r\n",struct_loc0.value9);
printf(usb_cdc_putc,"Valu10: %3u\r\n",struct_loc0.value10);
for(i=0;i<10;i++)
printf(usb_cdc_putc," %lu",struct_loc0.array1[i]);
printf(usb_cdc_putc,"\r\n");
for(i=0;i<10;i++)
printf(usb_cdc_putc," %lu",struct_loc0.array2[i]);
printf(usb_cdc_putc,"\r\n");
for(i=0;i<10;i++)
printf(usb_cdc_putc," %lu",struct_loc0.array3[i]);
printf(usb_cdc_putc,"\r\n");
}
}
void function2(testStructure struct_loc1){
debugState(struct_loc1);
}
void function1(testStructure *struct_loc2){
debugState(*struct_loc2);
function2(*struct_loc2);
}
void main()
{
setup_lcd(LCD_DISABLED);
setup_adc_ports(sAN0 | sAN1 | sAN7, VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64); //262 ms overflow, resolution 4us
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //32,7 ms overflow, resolution 0,5us
disable_interrupts(INT_TIMER0);
disable_interrupts(INT_TIMER1);
disable_interrupts(INT_EXT);
disable_interrupts(INT_RDA); // esp
enable_interrupts(GLOBAL);
usb_init();
usb_task();
int connectedUSB = 0;
testStructure struct_test = {5,10,2,1,0,100,10,0,7,1,
{5,5,5,5,5,5,5,5,5,5},
{200,200,200,200,200,200,200,200,200,200},
{200,200,200,200,200,200,200,200,200,200}
};
while(true){
usb_task();
if(usb_cdc_carrier.dte_present){
if(connectedUSB == 0){
connectedUSB = 1;
printf(usb_cdc_putc,"Test\r\n");
function1(&struct_test);
}
}
}
} |
but it seems I don't use strucute and pointer in the good way as i got in serial terminal :
Quote: | Test
Valu1 : 5
Valu2 : 10
Valu3 : 2
Valu4 : 1
Valu5 : 0
Valu6 : 100
Valu7 : 10
Valu8 : 0
Valu9 : 7
Valu10: 1
5 5 5 5 5 5 5 5 5 5
200 200 200 200 200 200 200 200 200 200
200 200 200 200 200 200 200 200 200 200
Valu1 : 5
Valu2 : 5
Valu3 : 5
Valu4 : 5
Valu5 : 5
Valu6 : 5
Valu7 : 5
Valu8 : 5
Valu9 : 5
Valu10: 5
1285 1285 1285 1285 1285 1285 1285 1285 1285 1285
1285 1285 1285 1285 1285 1285 1285 1285 1285 1285
1285 1285 1285 1285 1285 1285 1285 1285 1285 1285 |
thanks for your help
regards
Last edited by spilz on Mon Jun 15, 2020 7:40 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Mon Jun 15, 2020 7:36 am |
|
|
struct_loc is a pointer, not the structure.
To access an element referenced by a pointer, you use -> not '.'.
You also need to declare your debugState function to use the pointer:
(testStructure * struct_loc){
Last edited by Ttelmah on Mon Jun 15, 2020 7:39 am; edited 1 time in total |
|
|
spilz
Joined: 30 Jan 2012 Posts: 219
|
|
Posted: Mon Jun 15, 2020 7:38 am |
|
|
which one struct_loc ?
sorry my code is not clare like this, I chnage the struct_loc name |
|
|
spilz
Joined: 30 Jan 2012 Posts: 219
|
|
Posted: Mon Jun 15, 2020 7:42 am |
|
|
Ttelmah wrote: | struct_loc is a pointer, not the structure.
To access an element referenced by a pointer, you use -> not '.'.
You also need to declare your debugState function to use the pointer:
(testStructure * struct_loc){ |
As I don't want to modify the values in debugState, I wanted to pass the structure as parametter and not the pointer
maybe the wrong way ?
it's maybe not possible to switch from pointer to structure as I expected |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Mon Jun 15, 2020 7:44 am |
|
|
I think you are misunderstanding 'dereferencing'.
You are handing the function a pointer, not the actual structure. If you do this
you should just use the pointer in the functions.
If you want to hand the physical structure to another sub function, you
need to physically extract the data from the pointer.
You don't physically have a copy of the structure in your function to pass
to the sub functions. |
|
|
spilz
Joined: 30 Jan 2012 Posts: 219
|
|
Posted: Mon Jun 15, 2020 7:47 am |
|
|
but why does it work the first time in Code: | debugState(*struct_loc2); | but does not work the second time through Code: | debugState(struct_loc1); | ? |
|
|
spilz
Joined: 30 Jan 2012 Posts: 219
|
|
Posted: Mon Jun 15, 2020 7:52 am |
|
|
ok I think I understand,
it was random that it works sometime the first time, because compiler use the same part of memory |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Mon Jun 15, 2020 9:23 am |
|
|
Yes, 'random data arrival'...
A common CCSical condition!. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Mon Jun 15, 2020 11:55 am |
|
|
spilz wrote: | Ttelmah wrote: | struct_loc is a pointer, not the structure.
To access an element referenced by a pointer, you use -> not '.'.
You also need to declare your debugState function to use the pointer:
(testStructure * struct_loc){ |
As I don't want to modify the values in debugState, I wanted to pass the structure as parametter and not the pointer
maybe the wrong way ?
it's maybe not possible to switch from pointer to structure as I expected |
site note for you: in CCS if you want to pass a pointer to a value that you don't want to modify, you can use the _readonly flag:
Code: |
void debugState(_readonly testStructure * struct_loc0)
|
This tells the compiler that struct_loc0 points to a location that is readonly and will prevent modifications (NOTE: older versions of the compiler have a bug that makes this not work in some scenarios, but later versions fix this bug).
In ANSI C, it would be
Code: |
void debugState(const testStructure * struct_loc0)
|
But const has a different meaning in default CCS code.
EDIT: Example:
Code: |
#include <24FJ32GA002.h>
typedef struct {
int16 value1;
int16 value2;
} test_t;
void debug(_readonly test_t *t){
t->value1 = 16;
}
void main() {
while(TRUE);
}
|
gives the error in v5.093:
Code: |
*** Error 167 "main.c" Line 9(7,13): Assignment invalid: lvalue is READ ONLY
1 Errors, 0 Warnings.
Build Failed.
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|