|
|
View previous topic :: View next topic |
Author |
Message |
jaime
Joined: 25 Nov 2005 Posts: 56 Location: Porto - Portugal
|
Declaring variables - In the right place |
Posted: Sun Mar 22, 2009 12:02 pm |
|
|
Hello
I think its a basic question but...
Where should I declare the variables? In main()? In each function? In the top declaring it global?
Which one is the right place?
Thanks _________________ www.jagsilva.com |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah Guest
|
|
Posted: Sun Mar 22, 2009 1:50 pm |
|
|
Variables declared outsde the functions, are global. Should be declared before they are used.
Variables inside functions, should be declared at the start of the function, and are 'local' to that function. They are present (memory is reserved for them), inside the declaring function, and in all 'sub' functions. However their 'name', is only valid in the declaring function.
Variables 'die' (their memory is re-used), when the function exits, _unless_ the variable is declared as static.
So:
Code: |
int8 fred=3; //fred is global
int8 sub1(int8 val); //sub1, is a function, that is going to return an int8
void sub2(int8 *ptr); //and sub2
int8 sub3(void);
void main(void) {
int8 tom; //tom is a variable available in the main
tom=sub1(1);
//tom now equals 2 (1*2)
sub2(&tom);
//tom now back to 1
tom=sub3();
//tom now 1. The counter in sub3 is initialised to 0, then incremented.
tom=sub3();
//tom now 2
tom=sub3();
//tom now 3
tom=sub3();
//tom still 3, since 'ctr' is greater than the global 'fred'
}
int8 sub1(int8 val) {
int8 local; //local is a variable available inside sub1
local = val*2;
return local; //and result fed back to main
}
void sub2(int8 *ptr) {
//Now 'tom' still exists, since we are _inside_ the caller
//we have a pointer to it, and can modify it
*ptr=*ptr/2;
}
int8 sub3(void) {
static int8 ctr=0; //ctr is initialised to 0, on the _first_ call
ctr++
if (ctr>fred) ctr=fred;
return ctr;
}
|
Best Wishes |
|
|
jaime
Joined: 25 Nov 2005 Posts: 56 Location: Porto - Portugal
|
|
Posted: Mon Mar 23, 2009 1:01 am |
|
|
Thanks for your help.... and I have one more question about variables declared in main:
The space reserved for variables declared in main I'll be always reserved. The difference between main variables and global variables is that I need to pass a pointer if I want to use main variables in other functions, correct??
And if the program is in the main and calls a sub-function the variables used in main will be kept, correct?
My problem is that I need a buffer with 512 bytes (for writing in the mmc) and I don't want to lose the data. I don't use the buffer in main. I use it in two other functions but I need to declare it in global or in main if I don't want to lose the data... correct? so... a PROGRAMMER will declare it in main or in global??
Ttelmah wrote: | Variables declared outsde the functions, are global. Should be declared before they are used.
Variables inside functions, should be declared at the start of the function, and are 'local' to that function. They are present (memory is reserved for them), inside the declaring function, and in all 'sub' functions. However their 'name', is only valid in the declaring function.
Variables 'die' (their memory is re-used), when the function exits, _unless_ the variable is declared as static.
|
_________________ www.jagsilva.com |
|
|
Ttelmah Guest
|
|
Posted: Mon Mar 23, 2009 3:18 am |
|
|
Lots of different ways of dealing with this:
1) Declare it as global.
2) Declare it in main, and pass a pointer to the functions.
These are the two you have thought of.
3) Declare it in the first function as 'static', and return a pointer, which is then passed to the second function.
You could also use the malloc library, giving this enough space to hold the buffer, and anything else you want. However this then adds extra overhead.
Remember also, you could have a 'global' _pointer_ to the array, and declare the array either in main, or in a sub function. Set the pointer to it's location, and then not have to worry about handing pointers around.
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Mar 23, 2009 3:23 am |
|
|
You need to use a pointer if you want to alter the contents of the var in a sub function or if it is a complicated var such as a struct.
In your case you have 2 options, declare in main and pass a pointer to the array around (between functions) or declare as a global. As the data is shared between functions I would proberbly declare as a global in this case as it makes life easier for you.
There is no hard rule to follow, passing vars and pointers is safer, using locals uses less memory (usually). Globals makes some things easier. I try to keep the number of globals as low as possible without making life too dificult. |
|
|
jaime
Joined: 25 Nov 2005 Posts: 56 Location: Porto - Portugal
|
|
Posted: Mon Mar 23, 2009 3:16 pm |
|
|
Ok. I think I'm in the right way.
Thanks to all for your help.
jaime _________________ www.jagsilva.com |
|
|
|
|
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
|