View previous topic :: View next topic |
Author |
Message |
mp3fusion
Joined: 13 Dec 2004 Posts: 7
|
pointer addition |
Posted: Tue Feb 01, 2005 2:25 pm |
|
|
Hello everyone ...
I need to have a pointer addition so that it points to a new location in the memory
my code is something like this ...
Code: |
not_used_vehicle // pointer to some structure
vehicle_data_node_type // some structure
not_used_vehicle += sizeof(vehicle_data_node_type);
|
for the last operation i get invalid type converion !!!!!!!
this is completely valid in standard C
I even tested it with a constant value ... something like this
Code: |
not_used_vehicle += 11;
|
this also did not work
I mean I could try something like this :
Code: |
int temp_size;
temp_size = sizeof(vehicle_data_node_type)
for ( i = 0; i < temp_size; i++)
not_used_vehicle++;
|
but this is not really pretty
I also tried to decieve it by doing this
Code: |
long temp_addr = not_used_vehicle + sizeof(vehicle_data_node_type);
not_used_vehicle = temp_addr;
|
but I get the same error ....
please help me out .... I don't know what is wrong ...
maybe this compiler doesn't support pointer operations like this ...
but I know this is possible in standard c
I am using PIC18f8720
Thank you so much in advance |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Feb 01, 2005 2:37 pm |
|
|
If the pointer is declared with the right data type, you wouldn't need to increment or decrement by the size of the data. The compiler is smart enough to do it for you. If you really want help, then you need to post a complete small test program which would have the variable declarations.
Here is a test:
Code: |
.................... #include <16C781.h>
.................... //////// Standard Header file for the PIC16C781 device ////////////////
.................... #device PIC16C781
.................... #list
....................
.................... #use delay(clock=4000000)
.................... #fuses NOWDT, PUT
....................
.................... struct dacStruct
.................... {
.................... int8 var1;
.................... int8 var2;
.................... int8 var3;
.................... }test[10],*ptr_test;
....................
.................... void main (void)
.................... {
0004: CLRF 04
0005: MOVLW 1F
0006: ANDWF 03,F
0007: BSF 03.5
0008: BCF 1F.4
0009: BCF 1F.5
000A: MOVLW 00
000B: MOVWF 1D
000C: MOVLW 07
000D: BCF 03.5
000E: BSF 03.6
000F: MOVWF 19
0010: BSF 03.5
0011: BCF 03.6
0012: MOVF 05,W
0013: ANDLW 00
0014: MOVWF 05
0015: BCF 03.5
0016: BSF 03.6
0017: MOVF 19,W
0018: BCF 00.0
....................
.................... ptr_test = test;
0019: MOVLW 22
001A: BCF 03.6
001B: MOVWF 40
.................... ptr_test++;
001C: MOVLW 03
001D: ADDWF 40,F
.................... ptr_test += 2;
001E: MOVLW 06
001F: ADDWF 40,F
....................
.................... while(1 ) ;
0020: GOTO 020
.................... }
....................
....................
0021: SLEEP |
The size of the struct is 3 bytes
.................... ptr_test++;
001C: MOVLW 03
001D: ADDWF 40,F
and from the above code it works.
Adding 2 to a pointer should increment by 6
.................... ptr_test += 2;
001E: MOVLW 06
001F: ADDWF 40,F
and it does. |
|
|
mp3fusion
Joined: 13 Dec 2004 Posts: 7
|
|
Posted: Tue Feb 01, 2005 3:07 pm |
|
|
Thank you very much from your help ... it was really helpful
I actually found the problem my code ...
the pointer that I defined was inside a structure ...
Code: |
typedfe struct
{
.... // other data members
vehicle_data_node_type * not_used_vehicle // pointer
}vehicle_info_header_type;
vehicle_info_header_type vehicle_info_header;
// I had
void somefunc()
{
// some code here
vehicle_info_header.not_used_vehilce += (sizoeof(vehicle_data_node_type) * 8);
}
|
and I got the error that it expects numeric conversion
However I did this ...
Code: |
void somefunc()
{
vehicle_data_node_type * temp;
// some code here
temp = vehicle_info_header.not_used_vehicle;
temp += (sizeof(vehicle_data_node_type) * 8 );
}
|
and it worked ...
I still don't know why ...
your comment would be grately appreciated
Thank you so much in advance |
|
|
|