From: Robby on 4 Feb 2010 13:23 "Igor Tandetnik" wrote: > Well, do you need these five pieces of data separately for some other reason, or >do you just need the end result of the calculation? If you just need them once to >calculate this sys_osc value, then there's no reason to store them. I need the end result (CURR_SYS_OSC) in main.c and I need the OSC_PERIOD in another module. But as you can see in innitial post OSC_PERIOD depends on CURR_SYS_OSC !!! So that's why I would need to store only the 4 values so I can use OSC_PERIOD in another module. The fifth value is CURR_SYS_OSC, which I only need in main, but it would be nice if I can store that too cause I might just need that somewhere else in some othe module. I am so discouraged beacause I have coded this and everything works but I am not happy with the way I did it. I would have to show the whole picture of how it all ties into another module called myLibs.c. But I don't want to bother you or anyone else for that matter with all of that. But it would be the only way for me to explain in detail what I really want and what is going on. Right now I am trying to store the 4 pieces of information in 4 static extern variables so I can calculate the fifth variable which is CURR_SYS_OSC. I started doing tests to try to just store 2 ints via a macro (I know you said to use functions... but it bugs me as to why it wouldn't work with a macro) and this doesn't even work: see here: ==============test.h #ifndef TEST_H #define TEST_H #define Z() ( y = 3; \ a = 4 \ ) extern int y; extern int a; #endif // ==========================test.c include <stdio.h> #include "test.h" int y; int x; int main() { Z(); return 0; } =============================== I get the following error: 1>c:\_dts_programming\pic\_microchip_issues\simulated in vc++\define_macros\define_macros\define_macros\test.h(15) : error C2059: syntax error : ')' I tried the define satement as shown in many examples on the Internet, with curly brackets instead of round, with semie colons, without semie colons, with back slashes, without back slashes.... and I always get some type of error. You have to excuse me Igor, I am not that good with define macros that carry out some code, its all new to me. Thanks in advance for getting back to me! Sincere regards Rob
From: Igor Tandetnik on 4 Feb 2010 13:35 Robby <Robby(a)discussions.microsoft.com> wrote: > "Igor Tandetnik" wrote: > >> Well, do you need these five pieces of data separately for some >> other reason, or >do you just need the end result of the >> calculation? If you just need them once to >calculate this sys_osc >> value, then there's no reason to store them. > > I need the end result (CURR_SYS_OSC) in main.c and I need the > OSC_PERIOD in another module. But as you can see in innitial post > OSC_PERIOD depends on CURR_SYS_OSC !!! So, why not get_osc_period() { return 1.0 / get_sys_osc(); } > ==============test.h > #ifndef TEST_H > #define TEST_H > #define Z() ( y = 3; \ > a = 4 \ ) You can't enclose statements in parentheses. I mean, the macro doesn't care, it's just a piece of text, but when it's actually expanded and the resulting text compiled, it turns into (y = 3; a = 4 ) which doesn't make any sense. I'm also not sure what that backslash in front of a paren is supposed to do. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Robby on 4 Feb 2010 14:37 "Igor Tandetnik" wrote: > Robby <Robby(a)discussions.microsoft.com> wrote: > > "Igor Tandetnik" wrote: > > > >> Well, do you need these five pieces of data separately for some > >> other reason, or >do you just need the end result of the > >> calculation? If you just need them once to >calculate this sys_osc > >> value, then there's no reason to store them. > > > > I need the end result (CURR_SYS_OSC) in main.c and I need the > > OSC_PERIOD in another module. But as you can see in innitial post > > OSC_PERIOD depends on CURR_SYS_OSC !!! > > So, why not > > get_osc_period() { > return 1.0 / get_sys_osc(); > } Yes, I will try this. If I run into any problems I will post a new thread showing the sample with myLib.c module. > > ==============test.h > > #ifndef TEST_H > > #define TEST_H > > #define Z() ( y = 3; \ > > a = 4 \ ) > > You can't enclose statements in parentheses. I mean, the macro doesn't care, it's just a piece of text, but when it's actually expanded and the resulting text compiled, it turns into > > (y = 3; > a = 4 ) > > which doesn't make any sense. I am trying stuff I read in samples.... like for example at: http://en.wikipedia.org/wiki/C_preprocessor And so this doesn't work either: ==============test.h #ifndef TEST_H #define TEST_H #define Z(a,b) y = a; \ a = b; \ extern int y; extern int a; #endif ==========================test.c include <stdio.h> #include "test.h" int y; int x; int main() { Z(); return 0; } =============================== Regards Rob
From: Igor Tandetnik on 4 Feb 2010 14:51 Robby <Robby(a)discussions.microsoft.com> wrote: > And so this doesn't work either: > ==============test.h > #define Z(a,b) y = a; \ > a = b; \ > extern int y; The sequence of assignments looks suspicious. Note that 'a' in both places refers to the macro parameter, not to the variable 'a'. Also, because of the trailing backslash after "a = b;", the line "extern int y;" is also part of the macro. It doesn't look like you meant that. > int main() > { Z(); Z takes two parameters. You are trying to use it with none. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Robby on 4 Feb 2010 15:48 "Igor Tandetnik" wrote: > Robby <Robby(a)discussions.microsoft.com> wrote: > > And so this doesn't work either: > > ==============test.h > > #define Z(a,b) y = a; \ > > a = b; \ > > extern int y; > > The sequence of assignments looks suspicious. Note that 'a' in both places refers to the macro parameter, not to the variable 'a'. > > Also, because of the trailing backslash after "a = b;", the line "extern int y;" is also part of the macro. It doesn't look like you meant that. > > > int main() > > { Z(); > > Z takes two parameters. You are trying to use it with none. aaahhhrrhg! clumsy me! That's not what I meant! ooofff! Here you go. ==========test.h #define Z(a,b) y = a; \ e = b; // << no back slah here to end macro, no ??? extern int y; extern int e; ============test.c #include <stdio.h> #include "test_A.h" int y; int e; int main() {Z(1,2); return 0; } ============== This still gives errors. But is it even possible to assign a value to the extern variables while inside a macro? Or am I doing something that can't be done in C ? Anyhow I sort of gave on this route, I am more focusing in the two functions route you pointed out. Please see my last thread. All help appreciated! Rob
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Why is reference from unused function required? Next: simple reference "for each" question |