|
|
View previous topic :: View next topic |
Author |
Message |
Franco Guest
|
Problem with struct pointer, cannot access character array |
Posted: Wed May 26, 2004 12:32 am |
|
|
Hello,
I have definded the following struct and a pointer to this struct:
Code: | struct mystruct {
int myfirstint;
char myarray[10];
int mylastint;
} thisismystruct;
struct mystruct *mystructPtr;
.....
.....
|
Now I point the struct pointer to this struct and I want to access the struct elements using the struct pointer operator -> :
Code: | mystructPtr = &thisismystruct;
mystructPtr->myfirstint; // works fine, returns the correct value
mystructPtr->myarray[0]; // does not work, returns wrong value
mystructPtr->myarray[5]; // does not work, returns wrong value
mystructPtr->mylastint; // works fine, returns the correct value |
This works fine for the integers, but if I want to access the elements of the character array, I get completely wrong values. I had a look at the generated assembler code, and it looks wrong for the character array, because it seems to simple.
Does anyone out there have an idea?
Cheers Franco |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 26, 2004 10:32 am |
|
|
I made a test program, to check your problem.
It works fine with a 16F877, and PCM vs. 3.188.
The output on my terminal window is: A05B
Code: | #include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
struct mystruct {
int myfirstint;
char myarray[10];
int mylastint;
} thisismystruct;
struct mystruct *mystructPtr;
//===========================================
void main(void)
{
char c;
char i;
//--------
// Initialize the structure.
thisismystruct.myfirstint = 'A';
// Fill the array with ASCII chars '0' to '9'.
for(i = 0; i < 10; i++)
{
thisismystruct.myarray[i] = i + '0';
}
thisismystruct.mylastint = 'B';
//-------
// Display some elements of the structure.
mystructPtr = &thisismystruct;
c = mystructPtr->myfirstint;
putc(c);
c = mystructPtr->myarray[0];
putc(c);
c = mystructPtr->myarray[5];
putc(c);
c = mystructPtr->mylastint;
putc(c);
while(1);
} |
|
|
|
|
|
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
|