|
Besides printf, can anyone think of another example of variable number of parameters using ellipsis?
<br />
void printf( const char *format, ... );<br />
This is the only declaration type I can't really think of when to use it, and HOW. Anyone?
|
|
|
|
|
what do you want to know ?
only some functions that use ellipsis?
or how they do that ?
|
|
|
|
|
v2.0 wrote: what do you want to know ?
only some functions that use ellipsis?
or how they do that ?
A situation where I want to use it, and how. Okay, for example,
<br />
int addition(int a, ...);<br />
This function returns the sum of variable number of integers, now in the function definition, how do I reference the other integer? b, c, d, etc?
What I want to know is an example of a function prototype with its definition. Can you think of any?
Thanks
|
|
|
|
|
int addition (int first, ... ) {
int iResult = 0;
int i = 0;
va_list int_list;
va_start(int_list, first);
while( i != -1 ) {
iResult += i;
i = va_arg(int_list, int);
}
va_end(int_list);
return iResult;
}
|
|
|
|
|
Hey thanks. Now I have another question, where did va_list come from? printf function has it too, I've seen va_list, va_start and va_arg. Are they built-in variables? macros?
|
|
|
|
|
don't mind how they are implemented. only keep in mind that they are part of the C (so C++) standard, and thanks to this, you can use them on whatever plateform knowing that they'll work...
you simply have to #include <stdio.h> and <stdarg.h>
for more informations, you could read this[^]
ps: if my answer was what you needed, score it right...
|
|
|
|
|
v2.0 wrote: ps: if my answer was what you needed, score it right...
Okay, done.
|
|
|
|
|
|
My advice on ellipsis - Don't use them, there're crap. My gripe is that it is impossible to create a type safe function that uses ellipsis.
Steve
|
|
|
|
|
I have a minor problem. I have a bit field (see below). I also have a few places where I deal
with the different parts of it. I've recently had to change the size of one of the fields, and
have to find all the places I've hardwired the old size.
If this was an array of BOOL / bools, I could use the sizeof operator. Is there any way of
deriving the size of the fields simply from the code?
I know there are various brute force approaches, use NUM_LAYERS_OVERLAYS (etc) everywhere,
change to struct containing a load of BOOLs, or BYTES, but the purist in me is intrigued
as to whether there is a more elegant solution....
Suggestions please?
Iain.
union _Layers_
{
DWORD dwAll;
struct _Parts_
{
DWORD dwImage : 1;
DWORD dwMesh : 1;
DWORD dwShapeMatch : 1;
DWORD dwComments : NUM_LAYERS_COMMENTS;
DWORD dwSelections : NUM_LAYERS_SELECTIONS;
DWORD dwOverlay : NUM_LAYERS_OVERLAYS;
} Parts;
_Layers_ ()
{
dwAll = -1;
}
};
|
|
|
|
|
Iain Clarke wrote: I've recently had to change the size of one of the fields, and
have to find all the places I've hardwired the old size.
What do you mean by this? I use bitfields all of the time and I change them all of the time. I can not think of a reason to 'hardwire' the old size except in the structure definition.
When you use parts, you reference by variable name. When you use the whole word you use dwAll. I guess you could make changes to dwAll by using bit operations, but that could have been avoided by using your structure.
|
|
|
|
|
Bob Flynn wrote: When you use parts, you reference by variable name
Don't worry, I do know that. This structure hold bits toggling whether some display features are turned on or off in my software.
I also have a dialog box, which shows a column of checkboxes on one side, and the "type" of bit on the other. When I changed the number of bits for each parts, I needed to change the dialog box. I was thinking of a way for the dialog box to automatically recongnise the changes if I made more in 2 years time.
I have since thought of a solution (in the bath, so I haven't tried it yet).
_Layer_ L.
int nBits;
L.dwAll = -1;
nBits = L.Parts.dwComments;
nBits++;
nBits = log (nBits, 2);
I'm sure I haven't got the syntax of the log command right, but the principal is there.
This was more of a puzzle than a real problem, as I did have alternative (but less pretty) ways of solving the problem.
Thanks,
Iain.
|
|
|
|
|
OK, I see what you are going for.
I think what you have will work just fine. Perhaps instead of a log2 function you may find that simply finding the non zero bit by counting will be a faster operation.
i = 0;<br />
while ((i < 32) && ((src & 0x80000000) == 0))<br />
{<br />
src = src << 1;<br />
++i;<br />
}
This will tell you how many leading zeros are in your word, therefore providing the location of the first non-zero value.
|
|
|
|
|
I am trying to push a variable of type bobject into a vector, i get an assertion error when i try to push it. Does anybody know what the problem could be?
|
|
|
|
|
Show your code, especially how you declared your vector (be sure to check the 'Ignore HTML tags in the message' checkbox located below the posting edit box on the CP posting page), the line where you call push_back (or push_front) and then it might help if you also posted the lines that generated the actual assertion (will contaion the text ASSERT or VERIFY or some variation there of) that your debugger will jump to when you tell it to debug your program after the assertion happens.
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
The code which pushes the vector
bobject o=read_Obj(argv[i]);
o.active=1; //Active
o.picked=0;//Not picked
o.path=argv[i]; //save object path
---------------->store_normal(o);
glLoadIdentity ();
glGetFloatv(GL_MODELVIEW_MATRIX,o.m);
//initialize all variables
cout<<"in"<<endl;
----------------->ob.MyObjects.push_back(o);
the store_normal code is
void store_normal(bobject o)
{
for(int i =0;i<o.faces.size();i++)
{
//caculate the normal of the vertices
GLfloat x;
GLfloat y;
GLfloat z;
//vertex1 - vertex2
float a1= o.faces[i].second.x- o.faces[i].first.x;
float a2= o.faces[i].second.y- o.faces[i].first.y;
float a3= o.faces[i].second.z- o.faces[i].first.z;
//vertex2 - vertex3
float b1= o.faces[i].third.x-o.faces[i].first.x;
float b2= o.faces[i].third.y-o.faces[i].first.y;
float b3= o.faces[i].third.z-o.faces[i].first.z;
//calculate x y,and z cordinates for normal using cross product
x=(a3*b2)-(a2*b3);
y=(a1*b3)-(a3*b1);
z=(a2*b1)-(a1*b2);
//1st normal
o.vert[o.faces[i].v1].norm.x =o.vert[o.faces[i].v1].norm.x+x;
o.vert[o.faces[i].v1].norm.y =o.vert[o.faces[i].v1].norm.y+y;
o.vert[o.faces[i].v1].norm.z =o.vert[o.faces[i].v1].norm.z+z;
//2nd normal
o.vert[o.faces[i].v2].norm.x =o.vert[o.faces[i].v2].norm.x+x;
o.vert[o.faces[i].v2].norm.y =o.vert[o.faces[i].v2].norm.y+y;
o.vert[o.faces[i].v2].norm.z =o.vert[o.faces[i].v2].norm.z+z;
//3rd normal
o.vert[o.faces[i].v3].norm.x =o.vert[o.faces[i].v3].norm.x+x;
o.vert[o.faces[i].v3].norm.y =o.vert[o.faces[i].v3].norm.y+y;
o.vert[o.faces[i].v3].norm.z =o.vert[o.faces[i].v3].norm.z+z;
}
}
this assertion error occurs when i include store_normal(o);
|
|
|
|
|
You have still not supplied the crucial piece of information, what is the exact assertion? Just saying "an assertion happened" means nothing, it could be anything.
Learn to use the tools you have in front of you. Start the debugger, step through and into your code. Watch the values of all your variables, and when you hit an assertion look at the code and all the variables referenced in that code. At least one of the variables will have an invalid value. Figure out which one it is, and then use the call stack to backtrace to where that invalid value was set.
It can take a little bit of detective work, but you can do it. I can't do it with the information you have supplied so far.
Good Luck!
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
in visual c++6 ,how can i write /in/ or read /from/ any port like LPT?
|
|
|
|
|
|
i've a project build by C++ and include header from borland and other.
how can i open this project into project on .NET in C++?????
Militiaware
Faris Madi
|
|
|
|
|
create an empty project and import the sources in it.
note that you'll have some incompatibilities if the sources were not pure standard C++
|
|
|
|
|
I want to call a commandline exe, which puts some string into the console. I want to redirect this into a string or into a file.
I tried CreateProcess with pipeing(>) the output to a file, but for some reason its not working...
It would be helpful if i could get some info on different ways achiving the above.
|
|
|
|
|
|
Ola.
I have a little wtf here, which seems unsolvable by googling (I tried and did not find any clues):
I have an ostringstream strbuf.
I do the following:
strbuf.width(14);
strbuf << "????/????";
After that, strbuf.str() is called and written to an output-file.
If I put "????/????" into strbuf, "??????" is written to the file.
If I use "????//????" instead, "??/????" is written out.
So, there may be a special meaning to "?" or "/" or both when used in a ostringstream.
Can anyone enlighten me on this one?
Note: Yes, its a "forward slash" not a "backslash", exactly as used here.
Cheers,
Sebastian
--
Contra vim mortem non est medicamen in hortem.
|
|
|
|
|
It is called a Trigraph, a sequence of three characters that starts with two question marks that the compiler uses to generate punctuation marks. Its a C leftover from the days of 7 bit ASCII.
This is what is happening:
"????/????" - contains trigraph '??/' which translate to '\'
"??\????" - contains escape sequence '\?' which translate to a literal question mark
"??????" - final result
Lookup Trigraphs in MSDN.
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good - Use it!
-- modified at 10:48 Monday 20th March, 2006
|
|
|
|