Want struct parameter by default set to NULL
-
Hi, I was wondering if it is possible to set an struct parameter to NULL. Something like this:
typedef struct _myStruct
{
int a;
}myStruct;
...
//if i dont pass a parameter set the pointer to NULL
void func(myStruct *ms=NULL);
...
main()
{
myStruct mS;
...
func();
...
func(&mS);
...
}Is something like this possible... Thanks in advance...
-
Hi, I was wondering if it is possible to set an struct parameter to NULL. Something like this:
typedef struct _myStruct
{
int a;
}myStruct;
...
//if i dont pass a parameter set the pointer to NULL
void func(myStruct *ms=NULL);
...
main()
{
myStruct mS;
...
func();
...
func(&mS);
...
}Is something like this possible... Thanks in advance...
-
Hi, I was wondering if it is possible to set an struct parameter to NULL. Something like this:
typedef struct _myStruct
{
int a;
}myStruct;
...
//if i dont pass a parameter set the pointer to NULL
void func(myStruct *ms=NULL);
...
main()
{
myStruct mS;
...
func();
...
func(&mS);
...
}Is something like this possible... Thanks in advance...
yes, that's called "default arguments"
This signature was proudly tested on animals.
-
Hi, I was wondering if it is possible to set an struct parameter to NULL. Something like this:
typedef struct _myStruct
{
int a;
}myStruct;
...
//if i dont pass a parameter set the pointer to NULL
void func(myStruct *ms=NULL);
...
main()
{
myStruct mS;
...
func();
...
func(&mS);
...
}Is something like this possible... Thanks in advance...
C++
allows default arguments, see, for instance, [^].If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You are right, but i did it before posting the message. I knew it sounds like a common question, but i did not found any good solutions for my question. I found an article which uses contructors for struct, i want to avoid this solution, i just need to get this parameter NULL.... I know also that how to get default parameters if i use something like int, float... but i need this for structs which seems to have an other behaviour. :-/
-
C++
allows default arguments, see, for instance, [^].If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You are right, but i did it before posting the message. I knew it sounds like a common question, but i did not found any good solutions for my question. I found an article which uses contructors for struct, i want to avoid this solution, i just need to get this parameter NULL.... I know also that how to get default parameters if i use something like int, float... but i need this for structs which seems to have an other behaviour. :-/
-
yes, that's called "default arguments"
This signature was proudly tested on animals.
-
Oh ha,...sry Maybe my question was not clearly. I know how to handle default arguments(int,bool,float,double...)...but i dont know how to handle it for Structs!
It makes difference? For instance:
void foo(MyStruct mystruct=MyStruct());
doesn't fit your needs? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Oh ha,...sry Maybe my question was not clearly. I know how to handle default arguments(int,bool,float,double...)...but i dont know how to handle it for Structs!
-
It makes difference? For instance:
void foo(MyStruct mystruct=MyStruct());
doesn't fit your needs? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Very good point... but, i use structs which also contains structs and many other items, so it would look like this:
void func(
MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring" )
);Am i right?? I think so. AND another problem is, that if i want to check it in the function, if it is a default, so i have to provide an additional variable:
void func(
MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring",1 )
);
...
void func(
MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring",1 )
)
{
if(mystruct.isDefault==1)//isDefault->last parameter
{...}
else
{...}
}I hoped(and still hoping) there is an easier solution, but you are right, this would work.
-
Oh, okay,... void func(myStruct *ms); works fine. But with: void func(myStruct *ms=NULL); i got the error C2143: Syntax error: missing ')' before '=' Maybe i am totaly wrong, but i think, i got the error because setting a default value for a struct can not be done so easy.
-
Very good point... but, i use structs which also contains structs and many other items, so it would look like this:
void func(
MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring" )
);Am i right?? I think so. AND another problem is, that if i want to check it in the function, if it is a default, so i have to provide an additional variable:
void func(
MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring",1 )
);
...
void func(
MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring",1 )
)
{
if(mystruct.isDefault==1)//isDefault->last parameter
{...}
else
{...}
}I hoped(and still hoping) there is an easier solution, but you are right, this would work.
Wischkony wrote:
use structs which also contains structs and many other items, so it would look like this: void func( MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring" ) ); Am i right?? I think so.
Yes, you may also override the MyStruct's default constructor to take care about.
Wischkony wrote:
AND another problem is, that if i want to check it in the function, if it is a default, so i have to provide an additional variable: void func( MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring",1 ) );...void func(MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring",1 ) ){if(mystruct.isDefault==1)//isDefault->last parameter {...}else {...}}
Provided you've overridden the default MyStruct's constructor the way I told, you may also use the following test (you've also to override the
==
operator, of course):if ( mistruct == MyStruct() )
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Wischkony wrote:
use structs which also contains structs and many other items, so it would look like this: void func( MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring" ) ); Am i right?? I think so.
Yes, you may also override the MyStruct's default constructor to take care about.
Wischkony wrote:
AND another problem is, that if i want to check it in the function, if it is a default, so i have to provide an additional variable: void func( MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring",1 ) );...void func(MyStruct mystruct=MyStruct(MyStruct2(),MyStruct3(),2,3,4,"anystring",1 ) ){if(mystruct.isDefault==1)//isDefault->last parameter {...}else {...}}
Provided you've overridden the default MyStruct's constructor the way I told, you may also use the following test (you've also to override the
==
operator, of course):if ( mistruct == MyStruct() )
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]->Yes, you may also override the MyStruct's default constructor to take care about. Yes, i know :) By reading and writing answers, i am thinking more and more that this is a good solution and not a bad solution. I was dreaming of an easy way like this: void func(mystruct *ms=NULL); But maybe this will stay a dream ;) Thanks a lot for your time.
-
->Yes, you may also override the MyStruct's default constructor to take care about. Yes, i know :) By reading and writing answers, i am thinking more and more that this is a good solution and not a bad solution. I was dreaming of an easy way like this: void func(mystruct *ms=NULL); But maybe this will stay a dream ;) Thanks a lot for your time.
Wischkony wrote:
void func(mystruct *ms=NULL);
Well, you have also that option. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Monday, December 15, 2008 11:53 AM
-
Oh, okay,... void func(myStruct *ms); works fine. But with: void func(myStruct *ms=NULL); i got the error C2143: Syntax error: missing ')' before '=' Maybe i am totaly wrong, but i think, i got the error because setting a default value for a struct can not be done so easy.
Wischkony wrote:
Maybe i am totaly wrong, but i think, i got the error because setting a default value for a struct can not be done so easy.
It has nothing to do with it being a
struct
. It has to do with the file having a.c
extension."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Hi, I was wondering if it is possible to set an struct parameter to NULL. Something like this:
typedef struct _myStruct
{
int a;
}myStruct;
...
//if i dont pass a parameter set the pointer to NULL
void func(myStruct *ms=NULL);
...
main()
{
myStruct mS;
...
func();
...
func(&mS);
...
}Is something like this possible... Thanks in advance...
i have to apologize to all here trying help me. I was running for hours against a wall. i tried the following: void func(mystruct *ms=NULL); and got an error, so thought this is not possible!! But it is possible, with: void func(mystruct *ms); void func(mystruct *ms=NULL) { ... } I am sry,...aeh this is embarrasing...
-
i have to apologize to all here trying help me. I was running for hours against a wall. i tried the following: void func(mystruct *ms=NULL); and got an error, so thought this is not possible!! But it is possible, with: void func(mystruct *ms); void func(mystruct *ms=NULL) { ... } I am sry,...aeh this is embarrasing...
Wischkony wrote:
I am sry,...aeh this is embarrasing...
Heh. No problem. No better way to find silly mistakes and easy solutions than putting it out there for the world to see :)
Mark Salsbery Microsoft MVP - Visual C++ :java: