CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

"typedef struct" is not a pointer?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

"typedef struct" is not a pointer?
PostPosted: Wed Mar 30, 2011 5:03 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 30, 2011 5:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 30, 2011 5:59 pm     Reply with quote

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? Shocked
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 30, 2011 7:29 pm     Reply with quote

In this Wikipedia article, they get the address of the my_point structure
by putting an & in front of it:
Quote:
&my_point;

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:
Code:
 MyFunc(TestMe);
 


If I change it to this:
Code:
 MyFunc(&TestMe);

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

View user's profile Send private message

PostPosted: Wed Mar 30, 2011 11:37 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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