|
|
Ahhh! Thanks.
I Dream of Absolute Zero
|
|
|
|
|
The reason given in that article is actually pretty poor. Yes, it forces the programmer to use a trailing semi-colon, but that isn't necessary (mostly, they do that simply for looks). The real reason for enclosing it in a do-while loop is to prevent naming conflicts.
For example, lets say you have the following macro:
#define SWAP_INT(a, b) \<br />
int c = a; \<br />
a = b; \<br />
b = c;
Now, your code looks something like the following:
void main()<br />
{<br />
int a = 1, b = 2, c = 0;<br />
SWAP_INT(a, b);
}
This creates a hard to find compiler-error that will annoy you (and anyone else trying to use that macro with a variable named 'c').
To prevent this, you could use the scoping operator:
#define SWAP_INT(a, b) \<br />
{ \<br />
int c = a; \<br />
a = b; \<br />
b = c; \<br />
}
However, some older C++ compilers do not treat this correctly (or even handle it at all. Thus, you can create a single loop do-while to handle the scoping:
#define SWAP_INT(a, b) \<br />
do { \<br />
int c = a; \<br />
a = b; \<br />
b = c; \<br />
} while (0);
The trailing ; is purely optional. If you leave it off, you force the programmer to place it themselves (thus treating it like a function call). Putting it there doesn't hurt anything though.
As a side note, declaring it the way they did (as a macro) is poor programming practice. It really should have been declared as a function. If the user passes an int as the "logger", they will get some weird compiler error messages dealing with types and unless they search through the code to find the macro defintion, will have no idea why it is happening.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
-- modified at 11:53 Thursday 1st June, 2006
|
|
|
|
|
Very clearly explained. Thank you.
You got my 5.
I Dream of Absolute Zero
|
|
|
|
|
An excellent explaination! However, I will offer one more use of the do/while(0) block. I have seen it used as a way to provide goto -like functionality without actually using the word goto .
For example, if you needed to jump out of the middle of the block, simply executing a break will take you to the bottom of the block, simulating a goto to a label at the end of the block.
It was a bit confusing the first time I saw code that used the do/while(0) like that...
[Edit: Note, I do not do this myself, and would use (and have used) used a goto in place of something like that...]
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
And in situations like that, exactly, I favor the goto - at least I know what is going on logically. The do{ }while(0); crap I really have to think about for a minute - to realize they just did not want to use the goto.
People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks
|
|
|
|
|
James R. Twine wrote: An excellent explaination! However, I will offer one more use of the do/while(0) block. I have seen it used as a way to provide goto-like functionality without actually using the word goto.
For example, if you needed to jump out of the middle of the block, simply executing a break will take you to the bottom of the block, simulating a goto to a label at the end of the block.
It was a bit confusing the first time I saw code that used the do/while(0) like that...
People that code like that do so for job-security. You should NEVER do that. If you actually look at the assembly code produced by the compiler when you write a do-while(0) with a break instead of just using goto, you'll notice it is exactly the same as when you do write a goto.
Although, your reasoning does offer another good reason for making this a function instead of a macro. Instead of a goto or break, you just return if you get to a state that needs such. Much cleaner, and you won't have someone asking you what the heck you are doing when they have to maintain your code.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
|
Dear all,
How to display a SDL screen in MFC appication. Is it possible to display in the dialog ...
Manjunath S
GESL
Bangalore
|
|
|
|
|
Whats SDL screen ?
whitesky
|
|
|
|
|
I saw a thread like that some times ago. Let me see...
Ah ok, I got it. It's here[^] and here[^]
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
Thank you Cedric now i see that sdl is previous message!!
whitesky
|
|
|
|
|
|
|
ok, so here is what i have to say to you.
you idiot. forget that
you don't listen what we answered to you. still you are asking the same question exactly the same way (ya, one thing you know is copy/paste).
you definitely lost my support on that topic (even if i didn't really know how to help you), but abusing of our time and patience will never help you nowhere in your life. no one is paid over Codeproject to answer ausive people, so we don't owe you anything!!
if you want an answer, start asking it correctly. then, don't repeat yourself ; everybody here saw that you asked the same question thrice...
man, all you deserve is get flamed
i repeat myself once again. its seems that i'm not the only one here not to know what SDL is, but still you re ask your sh*t without much explainations.
google yourself, and come back here when you have a true question about a specific problem; not ask people to do a whole job for you
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-- modified at 13:25 Thursday 1st June, 2006
|
|
|
|
|
toxcct wrote: you idiot.
That's real professional. If it bothers you so, why not simply ignore him? No one deserves to be called an idiot, regardless of their action(s).
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
after 2 warnings, i think he searched such a reaction...
btw, i assume the entiere responsibility of my words.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
toxcct wrote: btw, i assume the entiere responsibility of my words.
Thats MAN word
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I'm absolutly frustrated!
could a fresh set of eyes tell me why i'm getting this error:
(line 55) error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
int main ()
{
CString sentence;
CString str("1");
CString str1("2.1");
CString num('\xDB');
char spc = ' ';
sentence = str + num + num + str1 + num + num;
for(int index = 0; index < (sentence.GetLength()); index++)
{
if(sentence[index] == num)
strcpy( (sentence[index]), spc); //line 55
}
printf ("%s \n",sentence);
return 0;
}
Thanks so much... Confused & Frustrated
|
|
|
|
|
The first parameter of strcpy function must be of pointer type, char * , but you supply sentence[index] , which is a character of the string, not a pointer.
From the fragment, it seems that you need to replace a character with another one. For this task, try to use the Replace member function instead of your for loop:
<br />
sentence.Replace(num, spc);<br />
-- modified at 11:22 Thursday 1st June, 2006
|
|
|
|
|
Dear Kitty!
U cannot copy a character to a string!!!
Instead make that space ' ' as " "(i.e, character as string constant)..
Then hope fully it should work..
Regards
Karthick
Karthick. G
|
|
|
|
|
kitty5 wrote: for(int index = 0; index < (sentence.GetLength()); index++)
{
if(sentence[index] == num)
strcpy( (sentence[index]), spc); //line 55
}
The above code is VERY VERY bad! Unfortunately, your compiler won't tell you exactly how bad it is, but I'll give you an alternative:
Replace the entire for-loop with the following:
sentence.Replace(spc, _T(' '));
A better solution would be to re-write the entire function to the following:
<br />
int main ()<br />
{<br />
CString sentence;<br />
sentence.Format("1 2.1 ");<br />
printf("%s \n", (const char*)sentence);<br />
return 0;<br />
}<br />
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks,
Actually I was trying to simulate the output of a load after asking for an input and a current value(in amps) and I'm suppose to get like 0 and 0.5.
But the device kicks out 0'\xDB''\xDB'0.5'\xDB''\xDB'
instead of 0 0.5 .
And sscanf doesn't know what to do with '\xDB'.
I need to just get the 0 and 0.5.
Kitty
|
|
|
|
|
Unless you know exactly how many numbers you will have in there, sscanf isn't a wise choice. You can have strtok do it for you, but the CString::Replace method will work and look cleaner.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
kitty5 wrote: And sscanf doesn't know what to do with '\xDB'.
Right, sscanf() does not handle multibyte hexadecimal characters.
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|