Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Want struct parameter by default set to NULL

Want struct parameter by default set to NULL

Scheduled Pinned Locked Moved C / C++ / MFC
19 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CPalliniC CPallini

    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]

    W Offline
    W Offline
    Wischkony
    wrote on last edited by
    #7

    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!

    CPalliniC B 2 Replies Last reply
    0
    • M Maximilien

      yes, that's called "default arguments"

      This signature was proudly tested on animals.

      W Offline
      W Offline
      Wischkony
      wrote on last edited by
      #8

      :) okay, i did not knew that they were called default arguments. (I thought default parameter was correct...) But how can i handle structs??? I want to set an struct argument to NULL by default... Hm...

      1 Reply Last reply
      0
      • R ramana g

        i used a pointer to class as default parameter in c++. i dont know about c

        W Offline
        W Offline
        Wischkony
        wrote on last edited by
        #9

        You are right, classes would work, but it is not an option for me :( Anyway, thank you

        1 Reply Last reply
        0
        • W Wischkony

          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!

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #10

          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]

          In testa che avete, signor di Ceprano?

          W 1 Reply Last reply
          0
          • W Wischkony

            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!

            B Offline
            B Offline
            BonshatS
            wrote on last edited by
            #11

            It should work the same way. What error are you getting that it doesn't?

            W 1 Reply Last reply
            0
            • CPalliniC CPallini

              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]

              W Offline
              W Offline
              Wischkony
              wrote on last edited by
              #12

              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.

              CPalliniC 1 Reply Last reply
              0
              • B BonshatS

                It should work the same way. What error are you getting that it doesn't?

                W Offline
                W Offline
                Wischkony
                wrote on last edited by
                #13

                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.

                D 1 Reply Last reply
                0
                • W Wischkony

                  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.

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #14

                  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]

                  In testa che avete, signor di Ceprano?

                  W 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    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]

                    W Offline
                    W Offline
                    Wischkony
                    wrote on last edited by
                    #15

                    ->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.

                    CPalliniC 1 Reply Last reply
                    0
                    • W Wischkony

                      ->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.

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #16

                      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

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      • W Wischkony

                        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.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #17

                        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

                        1 Reply Last reply
                        0
                        • W Wischkony

                          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...

                          W Offline
                          W Offline
                          Wischkony
                          wrote on last edited by
                          #18

                          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...

                          M 1 Reply Last reply
                          0
                          • W Wischkony

                            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...

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #19

                            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:

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups