Home TechnologyProgrammingC++ Subscribe to RSS

How To Call A Function Again In Case Statement In C Language?

If there are two cases. Case1 has a call to function a() and case2 has a call to function b(). Now I want to access the function a() in case2 without declaring its name {a()} mean calling the function not by its name but some other method.

Answer Question

2 Answers - Sort by: Date | Rating

    If you need to call a() as the last thing in case 2, you could code your switch statement with Case 2 first, then Case 1 below it. If you then avoided calling break at the end of the Case 2 code, execution would continue on into the Case 1 code, thus calling a(). Of course, if there were any more cases below Case 1, their code would be called too unless you put a break after the a() in Case 1. Also it doesn't work if you need to do something in Case 2 after calling a(). It's incredibly bad coding style, too, but if, for some reasons, you really wanted to do that, it might be what you need.
    0 0

    Paul_uk 

    answered 1 year ago

      As I under stand you want to call function from function b but not use the normal a() method to call. Correct?
      Well you could call using a pointer to the function. I do not have access to a compiler here but the code would look something like this:

      static void a(char message);
      void (*ptr2a)(char) = a;
      static void b(void );
      void main( )
      {
      for(int x = 0; x < 2; ++x)
      {
        switch(x)
        {
        case 0 : A((char)"Good Morningn");
         cycle;
         
        case 1 : B();
         cycle;
        }
      }
      }
      void a(char message)
      {
      printf("%s",message);
      }
      void b(void)
      {
      ptr2a((char)"and good night n");
      }
      0 0

      Jnawrocki 

      answered 1 year ago

      Don't know why the capital A and B in the case statements. It is lower case in the code. But then blurtit has their own formatting rules.
      Report
      Jnawrocki

      Jnawrocki

      commented 1 year ago

        Answer Question - Answers are editable for 5 min.

        If you do not Sign-in or Register your answers will be anonymous,

        your answers may also be checked before going online.

        More

        More

           
           

          Ask a Question via Twitter

          Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

          Blurtit Store

          Get T-shirts, hoodies, caps and more at the Blurtit store

          Blurtit International