|
|
View previous topic :: View next topic |
Author |
Message |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
making a function accept multiple characters |
Posted: Sun Aug 26, 2012 4:13 pm |
|
|
compiler 4.033
pic18f14k22
I feel like this should be possible, but I've tried everything that makes sense to me and searched the manual up and down, there seems to be no simple solution.
I'm trying to make a function that will accept multiple characters, something like this:
Code: |
void name_sensor(int8 number, char data[4])
{
int8 counter;
for(counter=0;counter<4;++counter)
{
flash_write(number,data[counter])
}
} |
Then to call the function, I want to be able to do something like this:
name_sensor(0,'R','a','i','n');
or even this:
char name[]='R','a','i','n'
name_sensor(0,name[]);
But obviously that doesn't work because name[] needs a constant to tell it which item in its array to point to.
Any ideas on how I can make a function accept multiple characters? I want to use a byte array as the char variable "char name[4]" so I can use a counter to increment to the next element in the function loop.
This is a compiler capability specific question so I don't think posting the errors I'm getting with each method is necessary, though I'm willing to re-compile everything I've tried and list the corresponding errors on here.
Cheers! _________________ Vinnie Ryan |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 26, 2012 6:01 pm |
|
|
Give it a pointer to an array and give it the number of (sequential)
elements in the array that you want to process. Look at the reset()
function in the 2nd answer on this page for an example:
http://stackoverflow.com/questions/1106957/pass-array-by-reference-in-c
The other method you talk about in your post is called "Variable
Argument Lists". According to the CCS manual you can do this, but why ?
It's much more simple to do it the way I gave above. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Aug 27, 2012 8:56 am |
|
|
As a separate comment, what he wants to do, would work, if the declarations were correct:
Code: |
//header stuff
void name_sensor(int8 number, char data[]) {
int8 counter;
for(counter=0;counter<4;++counter) {
flash_write(number,data[counter])
}
}
void main(void) {
char name[]={'R','a','i','n'};
name_sensor(0,name);
//etc....
}
|
Key point to understand, is that in an argument list, ',' is the argument separator. In your declaration, you are declaring one array argument, and then trying to fill it with four separate arguments, rather than filling the four elements in the array. Note the {} brackets.
Then you only need to give the name of the array to the function. The function already 'knows' it is expecting an array.
Best Wishes |
|
|
|
|
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
|