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

Creating Functions

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



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

Creating Functions
PostPosted: Wed Jul 21, 2010 3:05 am     Reply with quote

Hi,

I'm trying to create a function but the compiler (4.087) can't 'detect' my declares of the formal parameters and returns an Undefined Modifier error message. Where am I going wrong?

Code:

void WriteData(char Data1[4], char Data2[4], char Data3[4], char Data4[4], int PageNum); //Function Prototype



void WriteData (char Data1[4], char Data2[4], char Data3[4], char Data4[4], int PageNum) {

#include <string.h>

if (PageNum == 0){       //error here

//code in here

}

}



When I declare the variable PageNum inside the function the program compiles fine. I've checked it against the PICmicro CCS C book by Nigel Gardner and it matches up with no obvious differences....

Cheers,
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jul 21, 2010 3:16 am     Reply with quote

Move the #include line outside the function. It might be legal to do an include inside the function, but it is very uncommon. Problem is that in CCS the header files often contain code, so effectively you are inserting functions within your function which is illegal.
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed Jul 21, 2010 3:21 am     Reply with quote

Makes sense, thanks. Are the declarations right for the arguments?
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 21, 2010 5:38 am     Reply with quote

Geps wrote:
Makes sense, thanks. Are the declarations right for the arguments?


Probably. One thing I might suggest is to never, ever use int and short and the like in your programs. The problem is that an int tends to be 8 bits on most (maybe all) PIC chips but could be 16 bits, 32 bits, 9 bits, who knows what on other architectures. If you get into the habit of explicitly defining the size you will never be surprised. As such, you might think of changing all your "int" to "int8". This is probably a small deal for you right now but sometimes accidentally getting a variable of a different size than you planned on can be bad. Also, it makes it more clear when reading the code. I like this also because I do a lot of desktop development where ints tend to be either 16 or 32 bit so seeing int8 reminds me that I cannot be putting 16 bit values into that variable.
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed Jul 21, 2010 7:24 am     Reply with quote

Cheers collink.





I converted the type to Char and it compiles fine. Can anyone please explain how/why?
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 21, 2010 7:36 am     Reply with quote

How or why what? Why does it compile fine when you use char even though it's really an integer? Because char and int8 are the same type in CCS. Characters have, as far as I know, never been anything other than numeric types in C. Numeric types can be used as characters. There's not a whole lot of distinction. It's perfectly legal to do this:

Code:

int16 myVar;
myVar = 100 + 'C';


Geps wrote:
Cheers collink.
I converted the type to Char and it compiles fine. Can anyone please explain how/why?
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed Jul 21, 2010 7:39 am     Reply with quote

That's why I'm confused. My prototype and fucntion now read:

Code:
void WriteData(char Data1, char Data2, char Data3, char Data4, char PageNum);


And it compiles fine....As you say they're the same :S It's all rather confusing!


(The 'data's are no longer arrays as well but the problem wasn't with those.
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 21, 2010 7:45 am     Reply with quote

Geps wrote:
That's why I'm confused. My prototype and fucntion now read:

Code:
void WriteData(char Data1, char Data2, char Data3, char Data4, char PageNum);


And it compiles fine....As you say they're the same :S It's all rather confusing!


(The 'data's are no longer arrays as well but the problem wasn't with those.


Yes, it might be confusing at first but C allows you to treat integers as characters and characters as integer as you see fit. This allows for some good things. For instance, it makes switching between uppercase and lower case really easy.
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed Jul 21, 2010 7:48 am     Reply with quote

No sorry I meant why one version compiles and the other doesn't.
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 21, 2010 7:52 am     Reply with quote

Geps wrote:
No sorry I meant why one version compiles and the other doesn't.


Oh, sorry... Yes, that's strange. All you changed was int to char?
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Wed Jul 21, 2010 7:59 am     Reply with quote

Yup. I moved the code around onto my Main.c page, changed 'if else's to 'select case's and lots of other little things but the code that the lines between declaring and used the variable didn't change apart from the different type.

Maybe an internal bug that reset itself somehow? Hmmm......
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Jul 21, 2010 10:04 am     Reply with quote

Rememebr that if the type is 'char', as opposed to char[] or char*, the handling of the data in the function has to change. In the former, you receive a char value. In the latter cases, you receive a pointer _to_ a char value. Your use of the variables inside the function has to match the data arriving. It sounds as if it doesn't....

Best Wishes
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