View previous topic :: View next topic |
Author |
Message |
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
"typedef struct" is not a pointer? |
Posted: Wed Mar 30, 2011 5:03 pm |
|
|
Why does this little sample app not compile? :o
Code: | #include <18F87K22.h>
#device HIGH_INTS=TRUE, adc=16, ICD=TRUE
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HSM //Hi-Speed crystal oscillator
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPLLEN //No PLL enabled
#FUSES BBSIZ1K //1K words Boot Block size
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
typedef struct {
int16 var1;
int16 var2;
int16 var3;
}MyStruct;
MyStruct TestMe;
MyFunc(MyStruct *Foo);
void main (void)
{
TestMe.var1=1;
TestMe.var2=2;
TestMe.var3=3;
MyFunc(TestMe);
}
MyFunc(MyStruct *Foo)
{
Foo->var1=Foo->var2+Foo->var3;
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 30, 2011 5:55 pm |
|
|
Quote: | void main (void)
{
TestMe.var1=1;
TestMe.var2=2;
TestMe.var3=3;
MyFunc(TestMe);
}
|
Edit the line in bold to pass the address of the structure. |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Wed Mar 30, 2011 5:59 pm |
|
|
PCM programmer wrote: |
Edit the line in bold to pass the address of the structure. |
Uhm yes, but I'm confused, why isn't TestMe itself a pointer to the first element comparable to an array? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 30, 2011 7:29 pm |
|
|
In this Wikipedia article, they get the address of the my_point structure
by putting an & in front of it:
Link to Wikipedia article on Pointers to Structures
To show that this behavior is not confined to CCS, I made your program
into an MSVC++ 6.0 program (a C program). It gives me these messages:
Quote: |
error C2115: 'function' : incompatible types
warning C4024: 'MyFunc' : different types for formal and actual parameter 1 |
Both of those messages are about this line:
If I change it to this:
Then it loves it:
Quote: |
Compiling...
Test.c
Linking...
Test.exe - 0 error(s), 0 warning(s)
|
Here is the MSVC++ 6.0 test program (This is a C file, not a C++ file):
Code: |
// Test.c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int var1;
int var2;
int var3;
}MyStruct;
MyStruct TestMe;
MyFunc(MyStruct *Foo);
void main (void)
{
TestMe.var1=1;
TestMe.var2=2;
TestMe.var3=3;
MyFunc(TestMe);
exit(0);
}
MyFunc(MyStruct *Foo)
{
Foo->var1 = Foo->var2+Foo->var3;
} |
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Mar 30, 2011 11:37 pm |
|
|
Of course MyFunc(&TestMe); is the required syntax. TestMe represents the structure, not it's address.
You also can pass a structure as function parameter, if you want to.
The only case, where an object name can be used as a pointer is an array of characters, I think. |
|
|
|